Promises were one of the most exciting novelties of ES6. You might have used Promises before with libraries like Bluebird or Q, but they only became a standard JavaScript feature in 2015.
The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.
A Promise does something that can take some time, but eventually it will resolve if everything went well or reject if something went wrong. With _.then _and .catch you can then do something respectively with the result or with the error.
const promise = new Promise(function(resolve, reject) {
setTimeout(() => {
if (Math.random(1) > 0.5) {
resolve('Some value');
} else {
reject('Some error');
}
}, 500);
});
promise.then((response) => {
console.log(response);
}).catch(err => {
console.log(err);
});
They were introduced almost five years ago, so Promises shouldn’t be anything new to you, but ES2020 comes with a new feature: Promise.allSettled. It comes to fill a gap left by the ES6 Promise.all and Promise.race.
This tutorial covers Promise.allSettled functionality introduced in ES2020. Also, Promise.all has been compared with Promise.allSettled in this tutorial.
#es2020 #javascript #programming