FrontendInterviews.dev

Loading problem…

62. Promise.allSettled

Medium•

Implement a promiseAllSettled function that behaves like the native Promise.allSettled.

The function takes an array of promises (or values) and returns a single Promise that resolves after all inputs have settled (either fulfilled or rejected).

Unlike Promise.all, this function does not short-circuit on rejection. Instead, it waits for every input to complete and returns their outcomes.

Requirements

Core Behavior

  • Accept an array of Promises or values.
  • Return a single Promise.
  • Wait until all inputs have either resolved or rejected.

Output Format

  • The resolved value must be an array of objects describing the result of each input.
  • Each object should follow this format:
  • If fulfilled: { status: 'fulfilled', value: result }
  • If rejected: { status: 'rejected', reason: error }
  • The order of results must match the input order.

Edge Cases

  • Never Rejects: The returned Promise should always resolve, regardless of input failures.
  • Non-Promise Values: Treat values as already fulfilled Promises.
  • Empty Input: If the input array is empty, resolve immediately with [].

Example

const p1 = Promise.resolve(1);
const p2 = Promise.reject('error');
const p3 = Promise.resolve(3);

promiseAllSettled([p1, p2, p3]).then(results => {
  console.log(results);
  // [
  //   { status: 'fulfilled', value: 1 },
  //   { status: 'rejected', reason: 'error' },
  //   { status: 'fulfilled', value: 3 }
  // ]
});

Constraints

  • Promise.allSettled should never reject
  • Promise.allSettled should return array with status and value/reason
  • Handle non-promise values correctly
  • Handle empty arrays
Accepted21/21|Acceptance Rate100.0%