Implement myPromiseAll(promises)
A genuine senior-level question, it's easy to get the happy path working and much easier to miss the failure semantics that actually matter.
Write myPromiseAll(promises) that behaves like the real Promise.all:
- Returns a promise that resolves with an array of all resolved values, in the same order as the input, even though the individual promises may resolve out of order.
- Rejects as soon as any single promise rejects, with that promise's rejection reason (don't wait for the others).
- Resolves immediately with
[]for an empty input array.
await myPromiseAll([
Promise.resolve(1),
new Promise((r) => setTimeout(() => r(2), 100)),
Promise.resolve(3),
]);
// [1, 2, 3], in INPUT order, regardless of resolution order