Goal: Understand the difference between
Promise.all()andPromise.allSettled()and practice handling all promise outcomes.
Promise.all, your promiseAllSettled function should return a new Promise.Promise.allSettled never rejects. It waits for all promises to complete.results array and a settledCount counter.promises array. For each item:
Promise.resolve() for universal handling..then(), add an object like { status: 'fulfilled', value: ... } to results..catch(), add an object like { status: 'rejected', reason: ... } to results.then and catch (or in finally), increment the settledCount.settledCount equals the length of the promises array, call resolve() with the results array.const promiseAllSettled = (promises) => {
// Return a new promise. It will be resolved when all promises in `promises` are settled.
// Importantly, this promise will never be rejected, unlike Promise.all.
return new Promise((resolve) => {
// An array to store the final states of all promises.
const results = [];
// A counter to track the number of settled promises.
let settledCount = 0;
// The total number of promises for convenience.
const promisesCount = promises.length;
// If the input array is empty, immediately resolve the promise with an empty array.
if (promisesCount === 0) {
resolve([]);
return;
}
// Iterate over each item in the input array.
promises.forEach((promise, index) => {
// Wrap each item in `Promise.resolve()` to uniformly
// handle both promises and regular values.
Promise.resolve(promise)
.then(value => {
// If the promise is fulfilled,
// save its result as an object with status 'fulfilled'.
results[index] = { status: 'fulfilled', value };
})
.catch(reason => {
// If the promise is rejected,
// save the rejection reason as an object with status 'rejected'.
results[index] = { status: 'rejected', reason };
})
.finally(() => {
// The `finally` block executes in any case: after `then` and after `catch`.
// This ensures that the counter is incremented for each promise, regardless of its outcome.
settledCount++;
// If the number of settled promises equals the total number,
// it means all promises have completed.
if (settledCount === promisesCount) {
// Resolve the main promise, returning the array of results.
resolve(results);
}
});
});
});
};Solution Analysis:
You need to implement your own version of the built-in Promise.allSettled() static method.
The Promise.allSettled(iterable) method returns a promise that fulfills after all of the given promises have either fulfilled or rejected, with an array of objects that each describes the outcome of each promise.
For each outcome object, there is:
status string. Either 'fulfilled' or 'rejected'.'fulfilled', a value is present.'rejected', a reason is present.const p1 = Promise.resolve(3);
const p2 = new Promise((resolve, reject) => setTimeout(reject, 100, 'foo'));
promiseAllSettled([p1, p2]).then(results => {
/*
results will be:
[
{ status: 'fulfilled', value: 3 },
{ status: 'rejected', reason: 'foo' }
]
*/
console.log(results);
});promiseAllSettled.Promise.Goal: Understand the difference between
Promise.all()andPromise.allSettled()and practice handling all promise outcomes.
Promise.all, your promiseAllSettled function should return a new Promise.Promise.allSettled never rejects. It waits for all promises to complete.results array and a settledCount counter.promises array. For each item:
Promise.resolve() for universal handling..then(), add an object like { status: 'fulfilled', value: ... } to results..catch(), add an object like { status: 'rejected', reason: ... } to results.then and catch (or in finally), increment the settledCount.settledCount equals the length of the promises array, call resolve() with the results array.const promiseAllSettled = (promises) => {
// Return a new promise. It will be resolved when all promises in `promises` are settled.
// Importantly, this promise will never be rejected, unlike Promise.all.
return new Promise((resolve) => {
// An array to store the final states of all promises.
const results = [];
// A counter to track the number of settled promises.
let settledCount = 0;
// The total number of promises for convenience.
const promisesCount = promises.length;
// If the input array is empty, immediately resolve the promise with an empty array.
if (promisesCount === 0) {
resolve([]);
return;
}
// Iterate over each item in the input array.
promises.forEach((promise, index) => {
// Wrap each item in `Promise.resolve()` to uniformly
// handle both promises and regular values.
Promise.resolve(promise)
.then(value => {
// If the promise is fulfilled,
// save its result as an object with status 'fulfilled'.
results[index] = { status: 'fulfilled', value };
})
.catch(reason => {
// If the promise is rejected,
// save the rejection reason as an object with status 'rejected'.
results[index] = { status: 'rejected', reason };
})
.finally(() => {
// The `finally` block executes in any case: after `then` and after `catch`.
// This ensures that the counter is incremented for each promise, regardless of its outcome.
settledCount++;
// If the number of settled promises equals the total number,
// it means all promises have completed.
if (settledCount === promisesCount) {
// Resolve the main promise, returning the array of results.
resolve(results);
}
});
});
});
};Solution Analysis:
You need to implement your own version of the built-in Promise.allSettled() static method.
The Promise.allSettled(iterable) method returns a promise that fulfills after all of the given promises have either fulfilled or rejected, with an array of objects that each describes the outcome of each promise.
For each outcome object, there is:
status string. Either 'fulfilled' or 'rejected'.'fulfilled', a value is present.'rejected', a reason is present.const p1 = Promise.resolve(3);
const p2 = new Promise((resolve, reject) => setTimeout(reject, 100, 'foo'));
promiseAllSettled([p1, p2]).then(results => {
/*
results will be:
[
{ status: 'fulfilled', value: 3 },
{ status: 'rejected', reason: 'foo' }
]
*/
console.log(results);
});promiseAllSettled.Promise.The code editor is intentionally hidden on mobile.
Believe me, it's for the best: I am protecting you from the temptation to code in less-than-ideal conditions. A small screen and a virtual keyboard are not the best tools for a programmer.
📖 Now: Study the task, think through the solution. Act like a strategist.
💻 Later: Sit down at your computer, open the site, and implement all your ideas comfortably. Act like a code-jedi!