lecture 1 of 140 completed

Synchronous and asynchronous code

Some work finishes later, so the next line runs first. A value read before the work finished stays old.

What you'll learn

  • What synchronous and asynchronous mean
  • Why a value you already read does not update itself
  • How to tell which lines start work that finishes later

A test changes the due date of a loan in Shelf and then checks the date. The page shows the new date. The test reports the old one.

The examples in this track use Shelf, an invented library site. It is not a TestAcademy practice app. Here is the test, reduced to plain values:

let dueDate = "1 May";

function extendLoan() {
  setTimeout(() => {
    dueDate = "15 May";
  }, 20);
}

const before = dueDate;
extendLoan();
const after = dueDate;

console.log("before: " + before);
console.log("after: " + after);
before: 1 May
after: 1 May

Most lines you have written so far are synchronous. A synchronous line finishes its work before the next line starts.

extendLoan is different. It asks for the date to change in 20 milliseconds and returns at once. Work that is started now and finished later is asynchronous. Requests to a server and actions in a browser are asynchronous too.

So const after = dueDate runs while the date is still "1 May". The date changes a moment later, and nobody reads it again.

There is a second thing to see. after holds the text "1 May". When dueDate changes later, after does not follow it. A variable keeps the value it was given. It is not a live view of where the value came from.

The same mistake with a real page

This Playwright test has the same shape. The comment marks the line that reads too early.

import { test, expect } from '@playwright/test';

test('extending a loan changes the due date', async ({ page }) => {
  await page.goto('/loans');
  const dueDate = await page.getByTestId('due-date').textContent();  // read too early
  await page.getByRole('button', { name: 'Extend' }).click();
  expect(dueDate).toBe('15 May');
});

The address /loans is a relative path. Playwright puts the baseURL from its configuration file in front of it.

textContent() reads the text once and gives you a string. The click comes after it. The last line compares a string that was read before the click, so it can never see the new date. Waiting longer before the comparison would not help. The string is already in the variable.

The fix is to read after the work, and to let the check do the reading:

import { test, expect } from '@playwright/test';

test('extending a loan changes the due date', async ({ page }) => {
  await page.goto('/loans');
  await page.getByRole('button', { name: 'Extend' }).click();
  await expect(page.getByTestId('due-date')).toHaveText('15 May');
});

toHaveText reads the page again until the text matches or the time runs out. It is given the element to look at, not a string that was read earlier.

How to spot asynchronous work

Ask one thing about each line: does this start something that finishes later? In test code the usual cases are:

  • anything that talks to the browser, such as click, fill and goto
  • anything that talks to a server, such as an API request
  • a timer, such as setTimeout
  • any function that returns a promise, which the next lectures explain

A reviewer who sees a value read from the page and used several lines later will check what happened in between. If an action sits between the read and the check, the value is old.

Remember this

Asynchronous work is started now and finished later, and the next line does not wait for it. A variable keeps the value it was given and never updates itself. Read a value after the work that changes it.

Reference: Introducing asynchronous JavaScript on MDN.

now prove it

Reading is the setup. Solve this exercise in the editor. We run your test against the working practice app.