Unicorns VO Interview Question: Implement Promise.all | Coding Interview / Online Assessment

17 Views
No Comments

Implement Promise.all

Promise.all() takes an iterable of Promise objects and returns a single Promise. The returned Promise resolves to an array of results when all input Promises have resolved. It rejects as soon as any input Promise rejects, with the reason from the first rejected Promise.

Please implement a Promise.all() method.

Input Description

An array composed of multiple Promise instances.

Output Description

A Promise instance.

Example 1:

Input:  head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]

Example 2:

Input:  head = [1], n = 1
Output: []

Example 3:

Input:  head = [1,2], n = 1
Output: [1]

This problem asks you to implement the behavior of Promise.all. The key idea is to collect results in the original order, resolve only after every Promise succeeds, and reject immediately when the first Promise fails. A typical solution uses an index to preserve ordering plus a counter to detect when all promises have settled successfully.

END
 0