We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
A medium frontend problem, graded against 5 test cases (2 of them hidden).
React and browser-platform problems - hooks, event handling, and rendering behaviour.
Reach for it when you see: A component, hook, or DOM-behaviour question rather than an algorithm.
More Frontendproblems →```js
function promiseAll(promises) {
return new Promise((resolve, reject) => {
if (!Array.isArray(promises)) {
return reject(new TypeError("expected array"));
}
if (promises.length === 0) return resolve([]);
const results = new Array(promises.length);
let remaining = promises.length;
promises.forEach((p, i) => {
// Lift non-thenables; supports already-resolved values
Promise.resolve(p).then(
value => {
results[i] = value;
remaining -= 1;
if (remaining === 0) resolve(results);
},
reject // first reject wins; subsequent rejects are no-ops on the resolved Promise
);
});
});
}
```
Subtleties to call out in an interview:
- Order preservation: `results[i] = value` keeps input order even though promises settle in completion order.
- Fail-fast: the first `reject` rejects the outer promise; further settlements are ignored (Promises are single-resolution).
- Concurrency: all promises start immediately. There's no concurrency limit. `Promise.allSettled` is similar but never rejects (returns `[{status, value/reason}]`). `Promise.any` returns first fulfilled. `Promise.race` returns first settled (either kind).
- Iterables: the spec accepts any iterable, not just arrays. A complete polyfill would iterate `for (const p of iterable)`.
- Already-resolved values: `Promise.resolve(p)` lifts non-thenables to a fulfilled promise so you can `.then` on raw values uniformly.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
Read off this problem's own test suite, so these are the cases a submission actually has to survive.
These apply to the pattern as a whole, not just this problem.