FrontendPro

55Implement batch API calls with maximum batch size and interval

mediumAbout 35 minutesCodingbatchingpromisesperformance

Problem

A product list renders 60 cards and each card asks for its price: getPrice(id). The API has a batch endpoint, POST /prices with up to 25 ids. Turning 60 calls into 3 requests needs a batcher.

Implement createBatcher({ sendBatch, maxBatchSize = 25, maxWaitMs = 10 }). It returns:

  • load(item): returns a promise for that item's result.
  • flush(): send the current batch now.

sendBatch(items) receives an array of items and must resolve to an array of results in the same order. A batch is sent when it reaches maxBatchSize items or maxWaitMs after its first item was added, whichever comes first.

const prices = createBatcher({
  maxBatchSize: 25,
  sendBatch: (ids) => fetch("/prices", { method: "POST", body: JSON.stringify(ids) }).then((r) => r.json()),
});
const price = await prices.load(productId);

Requirements

  • Each load promise resolves with its own result from the batch.
  • If sendBatch rejects, every promise in that batch rejects with the same error. Other batches are unaffected.
  • If sendBatch returns a different number of results than items, reject that batch's promises with an error.
  • Several batches may be in flight at once.

Try it yourself

Write your solution, run it, then submit to check it against the tests.

solution.jsJavaScript

Press Run or ⌘ Enter to execute your code.

Step-by-step

load(item) returns a new promise and pushes { item, resolve, reject } onto the current batch.

Unlock the full solution

What's inside

  • Step-by-step
  • Approach
  • Solution
  • Complexity
  • Edge cases
  • Follow-up questions
  • Tests

Includes 2 code listings, 5 edge cases, 4 follow-up questions.

One-time payment by UPI, card or net banking. Launch price.

All 150 answersOne payment, yours forever