FrontendPro

37Implement Promise.allSettled()

mediumAbout 35 minutesCodingpromisesasyncpolyfills

Problem

Implement promiseAllSettled(iterable) without using Promise.allSettled. It returns a promise that always fulfils, once every item has settled, with an array of outcome objects in input order:

  • { status: "fulfilled", value } for items that fulfilled;
  • { status: "rejected", reason } for items that rejected.
const results = await promiseAllSettled([fetchProfile(), fetchFeed(), fetchAds()]);
for (const result of results) {
  if (result.status === "fulfilled") render(result.value);
  else logError(result.reason);
}

Requirements

  • Never reject, even if every item rejects.
  • Keep the input order.
  • Items may be promises, thenables or plain values; accept any iterable.
  • An empty input fulfils with [].

Try it yourself

Write your solution, run it, then submit to check it against the tests.

solution.jsJavaScript

Press Run or ⌘ Enter to execute your code.

Video walkthrough

Stuck? Watch someone solve it, then try again on your own.

25 Polyfill for promise methods all, allSettled, any & race | Javascript interview question in HindiCODERS NEVER QUIT on YouTubeOpen on YouTube

Step-by-step

Copy the iterable into an array, prepare a results array of the same length, and resolve [] immediately for an empty input.

Unlock the full solution

What's inside

  • Step-by-step
  • Approach
  • Solution
  • Complexity
  • Edge cases
  • Follow-up questions
  • Tests

Includes 2 code listings, 4 edge cases, 4 follow-up questions.

One-time payment by UPI, card or net banking. Launch price.

All 150 answersOne payment, yours forever