Promise
What is promise ?
Promise is an object representing the eventual completion or failure of an asynchronous
operation.
A JavaScript promise object contains both the producing code and calls to the consuming code. It can be
used to deal with asynchronous operation in JavaScript.
Promise State:
Pending: initial at state not get fulfilled or rejected
Fulfilled /Resolved: Promise Completed
Rejected: promise failed
A pending promise can either be resolved with a value or rejected with a reason error.
Promise Steps
var promise = new Promise(function(resolve, reject){
reject(8);
});
promise.then(add)
.then(sub)
.then(multi)
.then(divide)
.then(function(msg){
console.log('Result : ' +msg);
}).catch(function(err){
console.log('Error : ' +err);
});
Promise All
What is promise all ?
The promise all method return a single promise from list ofpromisese when all promises
fulfill.
var promise1 = Promise.resolve();
var promise2 = Math.random() > 8 ? 0 : Promise.reject();
var promise3 = new Promise((resolve, reject) => {
setTimeout(() => resolve('saten'), 1000)
});
Promise.all([promise1, promise2, promise3])
.then((val) => {
console.log(val)
})
.catch((val) => {
console.log(val);
})