AniUI Academy
medium+250 XPPractice

Reduce an array with an async reducer

Applying a queue of offline edits to a document, or importing rows one at a time so each can see the effect of the previous one, both need a fold where every step waits for the last.

Implement reduceAsync(items, reducer, initial). Starting from initial, call reducer(accumulator, item, index) for each item in order, awaiting the result before moving on, and resolve with the final accumulator. The reducer may return a plain value or a promise. An empty array resolves with initial without calling the reducer. If the reducer throws or rejects, reject with that reason and stop — later items are not processed.

What it has to do

  • Process items strictly in order, awaiting each result.
  • Pass (accumulator, item, index) to the reducer.
  • Resolve with initial for an empty array, without calling the reducer.
  • Support reducers that return plain values.
  • Reject on the first failure and stop processing.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

AI Crack & Solution Assist

Stuck? Get instant AI hints or break down the optimal solution.

Stuck? The javascript course covers everything this challenge needs.