advanced
Review: promiseAll that loses the order
This is recall. The code looks finished and is wrong. Find the defect and fix it. You are graded on tests you cannot see.
The batch loader ships with its own `promiseAll` — the standard library version was measured as too blunt, or too something, and a hand-rolled one came back. Its tests pass: three requests, resolved in order, results in order.
/**
* Resolves with every input's value in input order, or rejects with the
* first rejection. Plain values pass through in their slot.
*/
export function promiseAll<T>(values: Array<T | Promise<T>>): Promise<T[]> {
return new Promise<T[]>((resolve, reject) => {
const results: T[] = []
let pending = values.length
if (pending === 0) {
resolve(results)
return
}
values.forEach((value) => {
Promise.resolve(value).then(
(settled) => {
results.push(settled)
pending -= 1
if (pending === 0) resolve(results)
},
(reason: unknown) => reject(reason)
)
})
})
}review — find the defectCtrl+↵ submits · Ctrl+Shift+↵ runsstarter code