lecture 4 of 140 completed

async and await

What await pauses, what it does not pause, and what an async function gives back to its caller.

What you'll learn

  • What await does to the function it is in
  • Why an async function always returns a promise
  • Which code keeps running while a function is paused

A tester writes a helper that reads a late fee from the page and doubles it for a two-week loan. The helper uses await correctly. The test that calls the helper prints [object Promise].

function readFee() {
  return Promise.resolve(2);
}

async function feeForTwoWeeks() {
  const fee = await readFee();
  return fee * 2;
}

const total = feeForTwoWeeks();
console.log("total: " + total);
total: [object Promise]

Inside the helper everything is right. await readFee() waits for the promise and gives its result, the number 2. The helper returns 4.

The surprise is on the outside. A function marked async always returns a promise. When the function returns 4, the caller receives a promise that is fulfilled with 4. The language guarantees this for every async function, even one with no await inside.

So the caller has to wait as well:

function readFee() {
  return Promise.resolve(2);
}

async function feeForTwoWeeks() {
  const fee = await readFee();
  return fee * 2;
}

async function runCheck() {
  const total = await feeForTwoWeeks();
  console.log("total: " + total);
}

runCheck();
total: 4

This is why async and await spread upward through test code. A helper that awaits must be async. Its caller must await it and be async too, up to the test callback, which the test runner awaits for you.

await pauses one function, not the program

await pauses the function it is written in. Code outside that function keeps running.

function readFee() {
  return Promise.resolve(2);
}

async function showFee() {
  console.log("1. asking for the fee");
  const fee = await readFee();
  console.log("3. the fee is " + fee);
}

showFee();
console.log("2. showFee is paused, and this line runs");
1. asking for the fee
2. showFee is paused, and this line runs
3. the fee is 2
  1. showFee starts and runs like any function until its first await.
  2. At the await, showFee pauses and control goes back to the caller. The last line of the example runs.
  3. When the current code has finished and the promise has its result, showFee continues.

The language guarantees this order. The code after an await never runs in the same turn as the code before it, even when the promise is already fulfilled.

This is the reason a missing await on the outside is so quiet. The caller is not blocked. It carries on, and the paused work finishes later with nobody looking.

await on something that is not a promise

await accepts any value. With a plain value it gives that value back.

async function show() {
  const copies = await 3;
  console.log(copies);
}

show();
3

It works, and it hides nothing here. It is still worth removing. An await in front of a call tells the reader "this talks to the browser or a server". Putting it on plain values makes that signal useless. In Playwright, page.getByRole(...) needs no await, and adding one is the most common example.

Remember this

await pauses its own function until the promise is finished, and the caller keeps running. An async function always returns a promise, so its caller must await it too. Keep await for calls that really return a promise.

Reference: await on MDN.

now prove it

Reading is the setup. Solve this exercise in the editor. We run your code on each case and compare what it prints with the expected output.