Pointer Walk

Async/Await: In a Row or Together

Three awaits in a row wait three times. Start them together and the waiting overlaps.

Languages and RuntimesBeginner7 min read

Most of a program's time is spent waiting

A page needs three things from the network: the user, their orders, and today's offers. Each request takes a few hundred milliseconds. During that time your program is not computing anything. It has sent a question and is waiting for the answer.

async and await exist to make that waiting easy to write. They also make it easy to wait far longer than necessary.

Three awaits in a row

js
const user = await fetchUser();        // 300 ms
const orders = await fetchOrders();    // 200 ms
const offers = await fetchOffers();    // 400 ms

await means "pause this function here until the answer arrives". So the second request is not even sent until the first has come back, and the third waits for the second.

Three awaits in a rowInteractive
Code
fetchUser()
fetchOrders()
fetchOffers()
0300600900 ms

Start. The first call is sent. The code stops at the first await and does nothing else until the answer is back.

Time elapsed
0 ms

The total is 300 + 200 + 400 = 900 ms.

Look at the code again and ask: does fetchOrders need anything from fetchUser? Does fetchOffers need the orders? No. The three are independent. They were made to queue only because of how the lines were written.

Start them together

Switch the animation to "Start all, then await".

Calling an async function does not wait. It starts the work and immediately hands back a promise: a placeholder for a result that will arrive later. await is what does the waiting. So start all three first, and wait afterwards.

js
const [user, orders, offers] = await Promise.all([
  fetchUser(),
  fetchOrders(),
  fetchOffers(),
]);

All three requests are in flight at once. The total is the time of the slowest one: 400 ms.

Nothing got faster. The servers did the same work. Your program still waited for the same three things. The waiting overlapped.

The same in Python:

python
user, orders, offers = await asyncio.gather(
    fetch_user(),
    fetch_orders(),
    fetch_offers(),
)

Example

The rule of thumb

One after another: total time = the sum of the waits.

Together: total time = the longest wait.

With ten calls of 200 ms each, that is 2 seconds against 0.2 seconds.

When they must be in a row

Sequential awaits are correct when one step needs the result of the one before.

js
const user = await fetchUser(id);
const orders = await fetchOrders(user.accountId);    // needs user

Here there is no choice. The useful habit is to ask, for every await: does the next line use this result? If not, the two could probably run together.

Often it is a mix. Fetch the user first, then start everything that depends on the user at the same time.

A subtle version of the same bug

js
for (const id of ids) {
  results.push(await fetchItem(id));     // one at a time
}

An await inside a loop makes every pass wait for the one before. A hundred items take a hundred round trips, back to back. This is the async cousin of the N+1 query problem.

js
const results = await Promise.all(ids.map((id) => fetchItem(id)));

Things to watch when running together

  • One failure fails the lot. Promise.all rejects as soon as any one promise rejects. If you want every result regardless, use Promise.allSettled.
  • Do not start ten thousand at once. Launching a request for every item in a huge list can overwhelm the server you are calling, or your own connection limit. Run them in batches.
  • This is about waiting, not computing. Overlapping helps when the time is spent waiting on a network or a disk. Three heavy calculations in one JavaScript thread still run one after another.

Key takeaways

  • await pauses the function until a result arrives.
  • Awaiting independent calls one after another adds their waiting times together.
  • Calling an async function starts it. Start everything first, then await them together.
  • Together, the total is the longest wait, not the sum.
  • Keep awaits in sequence only when a step needs the previous result.

Your progress

Check your progress

0%

  1. Read the page50% of this page

    When you have gone through the page and its animations, tick it off.

  2. Take the quiz50% of this page

    6 questions, from easy to hard. Take it as often as you like: the questions change, and only your best score counts.

Saved on this device only. See all your progress