Prepin
Log in

U

Create a JavaScript Promise.all() function.

Approach / Explanation

This question requires you to create your own version of the JavaScript Promise.all() function. This function is part of the Promise API and is utilized when you have several promises and wish to execute a task only after all of them have been fulfilled. The Promise.all() method produces a single promise that resolves once all promises provided as an iterable have been resolved, or it rejects with the reason from the first promise that fails. Your implementation should mimic this behavior - it needs to accept an array of promises as an argument and return a new promise. This promise should resolve when all promises in the array have successfully resolved, and it should reject if any promise in the array fails. Here are some important aspects to keep in mind while implementing your function: Your function will accept an array of Promises. The function should return a new Promise. The promise returned by your function should resolve when all of the promises in the array have resolved. If any promise in the array rejects, the promise returned by your function should also reject. The value upon resolution should be an array that contains the resolved values of the input promises in the same sequence.

Suggested Answer

Here’s a rephrased version of the content while preserving its original meaning: ```javascript function promiseAll(promises) { return new Promise((resolve, reject) => { let resultArr = []; let promisesCount = promises.length; let fulfilledPromises = 0; promises.forEach((promise, index) => { Promise.resolve(promise).then(result => { resultArr[index] = result; fulfilledPromises++; if (fulfilledPromises === promisesCount) { resolve(resultArr); } }, reject); }); }); } ``` In this example, we begin by creating a new Promise. We initialize two variables: `resultArr`, which will store the resolved values, and `promisesCount`, which holds the total number of input promises. The `fulfilledPromises` variable keeps track of how many promises have been resolved. For each promise in the input array, we invoke `Promise.resolve()`. If a promise is successfully fulfilled, we store its result in `resultArr` at the correct index and increase the count of `fulfilledPromises`. Once all promises are fulfilled, we resolve the outer promise with `resultArr`.

Alternative Answer

In this example, we initiate a new Promise and iterate through the `promises` array, managing each promise using a `then()` method. When a promise resolves, its result is stored at the corresponding index in the `results` array, and the `counter` is decreased. If all promises have been resolved (meaning the `counter` reaches 0), the outer Promise is resolved with the collected results. Conversely, if any promise rejects, that rejection is passed directly to the returned Promise.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Flatiron Health
Uber

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.