JavaScript, inside out · Chapter 42 of 51

Async patterns

By the end of this chapter you can make network calls that survive a bad evening: try again after a failure, give up after a time limit, run only a few at once, and cancel work nobody needs any more. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Going async
  • 5 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.

Why you need this

It is the first monsoon evening with deliveries. Ravi's payment gateway fails one request in five, but works if you try again. The rider app sometimes takes forty seconds to answer. When he pressed "Find riders" for twenty orders at once, the rider service answered 429 Too Many Requests and blocked Tapri for ten minutes. And Anjali's menu search sometimes shows results for "ch" after the customer has typed "chai".

With what you know so far, await waits for one call, as long as the server likes, and one failure fails the whole order. Promise.all(orders.map(findRider)) starts all twenty requests in the same moment. And once a request is sent, nothing takes it back, so a slow old answer can land on top of a new one.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    retry: try again with growing pauses

    Write retry(fn, options). Call fn straight away; it returns a promise. If that promise fulfils, resolve with its value. If it rejects, wait and call fn again, up to retries more times. The pause doubles each time: baseDelay before the 2nd attempt, baseDelay times 2 before the 3rd, baseDelay times 4 before the 4th, and so on.

    options is { retries, baseDelay }, and both may be left out: the defaults are 3 retries and 100 ms, and retry(fn) with no options at all must work. With the defaults, an fn that always fails is called at 0, 100, 300 and 700 ms. When every attempt fails, reject with the error from the last attempt. When the first attempt works, fn is called only once.

  2. Task 2

    withTimeout: give up after a time limit

    Write withTimeout(promise, ms). If the promise settles within ms milliseconds, settle the same way: fulfil with its value, or reject with its error. If it has not settled by then, reject with an Error whose message is Timed out after 3000 ms (with the real ms in place of 3000).

    Whichever way it ends, clear your timer, so no timer is left behind: not after a fulfilled promise, and not after a rejected one either.

  3. Task 3 · interview question

    Interview: runLimited, at most N jobs at once

    Write runLimited(tasks, limit). tasks is an array of jobs: functions that start a piece of work and return a promise. Call each job exactly once, with at most limit of them running at any moment. Start limit jobs straight away, and as soon as any one finishes, start the next one in the list: do not wait for a whole batch.

    Resolve with all the results in the same order as tasks, not the order they finished. A limit bigger than the number of jobs is fine, and no jobs gives []. If any job rejects, reject with its error.

  4. Task 4

    cancellableDelay: a pause an AbortSignal can cancel

    Write cancellableDelay(ms, signal). Return a promise that resolves (with undefined) after ms milliseconds, unless the signal aborts first. The signal may be left out, and then it is a plain delay.

    If the signal aborts while waiting, clear the timer and reject with signal.reason itself (the same value, not a new error). If the signal is already aborted when you are called, reject at once with signal.reason and do not start a timer at all. A signal that never aborts changes nothing.

  5. Task 5

    makeLatestSearch: only the newest search counts

    Write makeLatestSearch(api). Return a search function. Each call search(query) aborts the signal it gave to the previous search (if there was one), then straight away calls api(query, signal) with a fresh AbortSignal that is not aborted, and returns whatever api returned.

    Each makeLatestSearch call makes an independent search box: searching in one never aborts the other's signal.

What you will learn

  • Retry with backoff: try again after a failure, waiting longer each time.
  • Promise.race: settle like the first promise to settle, which gives you a time limit.
  • A concurrency limit: only a few jobs in flight at once.
  • AbortController: a signal that tells work to stop.

More in Going async

Build "Async patterns" yourself

Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.