1. What are Promise APIs?
Promise APIs are built-in JavaScript methods for handling multiple promises simultaneously, allowing for efficient management of asynchronous operations. These APIs provide a way to aggregate and manipulate promises, helping developers coordinate multiple asynchronous tasks with ease.
2. Promise.all()
Promise.all()
takes an iterable (usually an array) of promises and returns a single promise that resolves when all the promises in the iterable have resolved or rejects if any promise in the iterable rejects.
- If all promises resolve, it returns an array of their resolved values.
- If any promise rejects, it returns the reason of the first promise that rejected, and the other promises are ignored.
const p1 = Promise.resolve(1);
const p2 = Promise.resolve(2);
const p3 = Promise.resolve(3);
Promise.all([p1, p2, p3])
.then((results) => {
console.log(results); // Output: [1, 2, 3]
})
.catch((error) => {
console.error(error);
});
// Example with rejection
const p4 = Promise.reject("Error occurred");
Promise.all([p1, p2, p4])
.then((results) => {
console.log(results); // This won't execute
})
.catch((error) => {
console.error(error); // Output: "Error occurred"
});
3. Promise.allSettled()
Promise.allSettled()
takes an iterable of promises and returns a single promise that resolves after all of the given promises have either resolved or rejected, with an array of objects that each describe the outcome of each promise.
- It does not short-circuit; it waits for all promises to settle (either resolve or reject).
- The returned array contains objects with
status
andvalue
(for resolved promises) orreason
(for rejected promises).
const p5 = Promise.resolve(1);
const p6 = Promise.reject("Error occurred");
const p7 = Promise.resolve(3);
Promise.allSettled([p5, p6, p7]).then((results) => {
console.log(results);
// Output: [
// { status: "fulfilled", value: 1 },
// { status: "rejected", reason: "Error occurred" },
// { status: "fulfilled", value: 3 }
// ]
});
4. Promise.race()
Promise.race()
takes an iterable of promises and returns a single promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects, with the value or reason from that promise.
- The returned promise will have the same outcome as the first promise that settles.
const p8 = new Promise((resolve) => setTimeout(resolve, 500, "First"));
const p9 = new Promise((resolve) => setTimeout(resolve, 100, "Second"));
Promise.race([p8, p9])
.then((result) => {
console.log(result); // Output: "Second" (resolves first)
})
.catch((error) => {
console.error(error);
});
5. Promise.any()
Promise.any()
takes an iterable of Promise objects and, unlike Promise.all()
, returns a single promise that resolves when any of the promises in the iterable fulfill. If no promises fulfill (i.e., all are rejected), it rejects with an AggregateError
, a new subclass of Error
that groups together multiple errors.
- It’s useful when you want to proceed as soon as one of the promises is resolved.
const p10 = Promise.reject("Error 1");
const p11 = Promise.reject("Error 2");
const p12 = Promise.resolve("Success");
Promise.any([p10, p11, p12])
.then((result) => {
console.log(result); // Output: "Success"
})
.catch((error) => {
console.error(error); // This won't execute
});
// Example with all rejections
Promise.any([p10, p11])
.then((result) => {
console.log(result); // This won't execute
})
.catch((error) => {
console.error(error); // Output: AggregateError: All promises were rejected
});