Deviopath › JavaScript

Build Your Own Promise.all

Hard · Async

Write a function promiseAll(items) that works like Promise.all. Promise.all, allSettled, any and race are blocked; Promise.resolve and new Promise are fine.

- items can mix Promises and plain values (a plain value counts as already resolved).

- Return a Promise that resolves with all results in the same order as items, once every item has resolved. An empty array resolves with [].

- As soon as any item rejects, reject with its error, without waiting for the others. If several reject, the first one in time wins.

testAll(jobs) is already written. A job { value, delay, fail } becomes a Promise that settles after delay ms; any other job is used as a plain value. It returns result (or the error message) and finishedBefore: the jobs that had settled when your Promise settled.

Next: Longest Palindromic Substring