Using Promises
Using Promises with Then() and Catch()
The then() and catch() methods are used to handle the results of Promises once they have finished pending. The then() method is used to handle resolved Promises while the catch() method is used to handle rejected Promises. Both of the methods use callback functions. The callback functions should each have one argument representing the Promise result.
Notice how the then() and catch() methods use callbacks to handle Promise results:
let promise = new Promise((resolve, reject) => {
//do stuff
let isSuccessful = true;
setTimeout(() => { //asynchronously process after 5 seconds
if (isSuccessful) { //if everything is successful
resolve('success!');
}
else{ //if something went wrong
reject(Error("failure."))
}
},5000);
});
//promise status changes from pending to resolved after 5 seconds
promise.then(val => {//val represents the fulfillment value
console.log(val);//logs "success!" since promise resolved
}).catch(val => {//val represents the rejection value
console.log(val); //doesn't occur since promise never rejected
});
Using Promises with Then(onSuccess, onFailure)
The then() method can be called with a success callback and a failure callback as an alternative to using the then() and catch() methods.
Notice how the then() method is used with a success and failure callback to handle promise results:
promise.then(val => {//success callback
console.log(val);
},val => {//rejection callback
console.log(val);
});