Problem, as an interviewer would state it:
"Implement reduceAsync(array, reducer, initial) — like Array.prototype.reduce, but the reducer function returns a promise, and each step must genuinely wait for the previous one before starting."
Examples:
await reduceAsync([1, 2, 3], async (acc, n) => acc + n, 0); // 6, computed sequentially
Clarifying questions expected:
- Must this genuinely run sequentially (each step awaiting the previous), or would running reducer calls concurrently and combining results differently be acceptable?
- What should happen with an empty array and an explicit initial value?
- Should the reducer also receive the index and the original array, matching the real synchronous
reducesignature?
Code / implementation expected: Yes — real, direct proof that the accumulator is correctly threaded through each step in strict sequential order, verified via a real, logged execution order.