Loading lesson path
Concept visual
Start at both ends
JavaScript promises. Use it to look up promise methods and common patterns. If you want to learn promises step by step, read JavaScript Promises.
Promise Object represents the completion or failure of an asynchronous operation. A Promise can have 3 states: pending initial state rejected operation failed fulfilled operation completed
Description.catch(onRejected) Provides a function to run when a promise is rejected.finally(onFinally) Provides a function to run when a promise is fulfilled or rejected.then(onFulfilled, onRejected) Provides two functions to run when a promise is fulfilled
Promise.all() Returns a single Promise from a list of promises
Promise.allSettled() Returns a single Promise from a list of promises
Promise.any() Returns a single Promise from a list of promises
Promise.race() Returns a single Promise from a list of promises
Promise.reject() Returns a Promise rejected with a given value Promise.resolve() Returns a Promise resolved with a given value Promise.then()
One funtion to run when a promise is fulfilled. One funtion to run when a promise is rejected. Promise.try() Executes a function and wraps its result in a promise. Promise.withResolvers() Returns an object containing a new Promise object and two functions to resolve or reject it. The .then() Method The.then() method provides two functions to run when a promise is fulfilled.
// Using then() to display a result myPromise.then(
function(value) {myDisplayer(value)}, function(value) {myDisplayer(value)}
);
The .catch() Method
The.catch()method provides a function to run when a promise is rejected.
// Using catch() to display a result myPromise.catch(
function(value) {myDisplayer(value);}
);
The .finally() Method
The.finally()method provides a function to run when a promise is fulfilled or rejected.
// Using finally() to display a result myPromise.finally(
function(value) {myDisplayer(value)}
);
Promise.resolve()Promise.resolve() method returns a Promise object resolved with a value.
let promise = Promise.resolve("Resolved OK");
promise.then(function(value) {
myDisplayer(value);
}).catch(function(value) {
myDisplayer(value);
});
Promise.reject()Promise.reject() method returns a Promise object rejected with a value.
let promise = Promise.reject("Something went wrong");
promise.then(function(value) {
myDisplayer(value);
}).catch(function(value) {
myDisplayer(value);
});
Promise.all()Promise.all() method returns a single Promise from a list of promises, when all promises fulfill (or rejects when one promise rejects).
let p1 = Promise.resolve("A");
let p2 = Promise.resolve("B");
Promise.all([p1, p2]).then(function(values) {
myDisplayer(values);
});
Promise.allSettled()
Promise.allSettled() returns an array of results for all promises.Each result contains a status and a value or reason.
Examples let p1 = Promise.resolve("A");
let p2 = Promise.reject("X");
Promise.allSettled([p1, p2]).then(function(results) {
myDisplayer(JSON.stringify(results));
});
// Create a Promise const myPromise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 200, "King");
});
// Create another Promise const myPromise2 = new Promise((resolve, reject) => {
setTimeout(resolve, 100, "Queen");
});// Settle All
Promise.all([myPromise1, myPromise2]).then(function(values) {
myDisplayer(values);
});
Promise.allSettled() means "Just run all promises. I don't care about the results".