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:
- Create a function named
promiseAll
that mimics the behavior of Promise.all
.
- 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:
- Handle non-promise values in the array by treating them as resolved promises.