Codetive
Sign In
Coding challenges as of now are only available on desktop
On mobile you can try our quiz
Quiz
We are working hard to bring coding challenges with editor on mobile 💚

Promise.all Polyfill

Objective: Implement a polyfill for Promise.all, which takes an array of promises and returns a single promise that resolves when all of the input promises have resolved.

Instructions:

  1. Create a function named promiseAll that mimics the behavior of Promise.all.
  2. The function should take an array of promises and return a single promise that:
    • Resolves with an array of resolved values when all promises are fulfilled.
    • Rejects immediately with the reason of the first promise that rejects.

Example Usage:

promiseAll([promise1, promise2, promise3])
  .then(values => console.log(values))  // All promises fulfilled, output an array of results
  .catch(error => console.error(error));  // One of the promises rejected

Requirements:

  • The promiseAll function should take an array of promises as input.
  • It should resolve with an array of resolved values if all promises resolve.
  • It should reject immediately with the reason of the first promise that rejects.
  • You cannot use the native Promise.all.

Hints:

  • Use Promise.resolve() and Promise.reject() to handle the result of each promise.
  • Return a new promise that resolves or rejects based on the input promises.

Bonus:

  1. Handle non-promise values in the array by treating them as resolved promises.
Browser
Console
Tests
Soon