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");
}
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
}
Thanks for reading my first post on dev.
Top comments (0)