Hone logo
Hone
Problems

Implement Promise.allSettled

JavaScript's Promise.allSettled method is a powerful tool for handling multiple promises concurrently. It allows you to execute several asynchronous operations and then receive a result that indicates the outcome of each promise, whether it fulfilled or rejected. This is crucial for scenarios where you need to know the status of all operations, even if some fail.

Problem Description

Your task is to implement a custom function, myPromiseAllSettled, that mimics the behavior of the built-in Promise.allSettled method. This function will accept an iterable of promises (or values that can be converted to promises) and return a single promise. This returned promise will resolve when all of the input promises have settled (either fulfilled or rejected). The resolved value of the returned promise will be an array of objects, where each object represents the outcome of a corresponding promise from the input iterable.

Each object in the result array should have the following structure:

  • If the promise was fulfilled: { status: 'fulfilled', value: the_fulfillment_value }
  • If the promise was rejected: { status: 'rejected', reason: the_rejection_reason }

Your implementation should preserve the order of the results corresponding to the order of the input promises.

Examples

Example 1:

const promise1 = Promise.resolve(3);
const promise2 = new Promise((resolve, reject) => setTimeout(reject, 100, 'foo'));
const promises = [promise1, promise2];

myPromiseAllSettled(promises).then((results) => {
  console.log(results);
});
[
  { "status": "fulfilled", "value": 3 },
  { "status": "rejected", "reason": "foo" }
]

Explanation: promise1 resolves with value 3. promise2 rejects with reason 'foo'. The output array reflects these outcomes in the same order.

Example 2:

const promiseA = Promise.resolve('hello');
const promiseB = Promise.reject(new Error('world'));
const promiseC = 'a string'; // A non-promise value

const promises = [promiseA, promiseB, promiseC];

myPromiseAllSettled(promises).then((results) => {
  console.log(results);
});
[
  { "status": "fulfilled", "value": "hello" },
  { "status": "rejected", "reason": "Error: world" },
  { "status": "fulfilled", "value": "a string" }
]

Explanation: promiseA fulfills, promiseB rejects, and promiseC (a string) is treated as an already fulfilled promise. The output array contains the respective outcomes.

Example 3: (Edge Case - Empty Iterable)

myPromiseAllSettled([])
  .then((results) => {
    console.log(results);
  });
[]

Explanation: When an empty iterable is provided, myPromiseAllSettled should resolve immediately with an empty array.

Constraints

  • The input iterable can contain any mix of Promises and non-Promise values. Non-Promise values should be treated as already fulfilled promises.
  • The function must return a single Promise.
  • The returned Promise must always resolve, never reject.
  • The order of results in the output array must correspond to the order of promises in the input iterable.
  • The function should handle up to 1000 input promises efficiently.

Notes

  • Remember that Promise.resolve(value) can be used to convert any value (including promises) into a Promise.
  • You will need to keep track of the state of each promise as it settles.
  • Consider how to manage the collection of results and trigger the final resolution of your myPromiseAllSettled function.
  • A common approach involves using a counter to track how many promises have settled.
Loading editor...
javascript