Javascript Promise
ScribbleTons

ScribbleTons @scribbletons

About: I am a full stack web developer. I use tools like laravel and vue to build applications for people. I love art.

Location:
Nigeria
Joined:
Oct 30, 2020

Javascript Promise

Publish Date: Nov 5 '20
2 0

Below here is a pseudo-code for javascript promise:

const myPromise = new Promise((resolve,reject) => {
if(condition is true){
resolve("Retrieve data from database");
}else{
reject("An error occured");
}
Enter fullscreen mode Exit fullscreen mode

As in life promises, something happens when a promise is fulfilled. You can achieve that with these lines of codes.

/*
When the resolve parameter of the promise constructor is resolved (processed and returns true) we can use 'then' property of Promise class to fulfill our promise.
*/
myPromise.then(res => { 
// do something with res data
});

//Similarly

/*
When the resolve parameter of the promise constructor is rejected due to some  (processed and returns false) we can use 'catch' property of Promise class to fulfill our promise but this time catching the error.
*/

myPromise.catch(error => {
//do something when promise is not resolved or display error
}
Enter fullscreen mode Exit fullscreen mode

Thanks for reading my first post on dev.

Comments 0 total

    Add comment