AniUI Academy
medium+250 XPPractice

Batch calls made in the same tick

When a list renders, each row may independently request its own data. Batching turns that burst into a single call by collecting every key requested in the current tick and flushing them together once the synchronous code finishes.

Implement createBatcher(handler), returning an object with a load(key) method. load returns a promise. Every key passed to load during the same synchronous run of code joins one batch; the batch is flushed on the microtask queue, so handler is called exactly once with an array of those keys in the order they were requested, duplicates included. handler returns a promise for an array of values in the same order, and each load promise resolves with the value at its own position. A key requested after a flush starts a new batch. If handler rejects, every load promise in that batch rejects with the same reason.

What it has to do

  • Call handler once per batch, with the keys in request order.
  • Include duplicate keys in the array rather than removing them.
  • Resolve each load with the handler value at the matching index.
  • Flush on the microtask queue, not with setTimeout.
  • Reject every load in the batch when handler rejects.

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.