A test borrows a book and then checks the status of the book. The borrow works. The check still sees the old status.
let status = "on shelf";
function borrowBook() {
return new Promise((resolve) => {
setTimeout(() => {
status = "on loan";
resolve();
}, 20);
});
}
borrowBook();
console.log(status);
on shelf
Until now, each line finished before the next one started. That stops being true when a line starts work that takes time, such as a request to a server or an action in a browser.
borrowBook stands in for that kind of work. setTimeout asks for a function to run later, here after 20 milliseconds. That later function changes the status.
borrowBook() does not wait for those 20 milliseconds. It starts the work and returns at once. What it returns is a promise. A promise is an object that stands for work that will finish later.
So the last line runs straight away, while the status is still "on shelf".
This order is guaranteed, and it does not depend on the 20. A timer callback never interrupts code that is running. That rule comes from the host, which is the browser or Node. The host runs a timer callback only after the current code has finished.
await waits for the promise
await in front of a promise pauses the function until that work has finished.
let status = "on shelf";
function borrowBook() {
return new Promise((resolve) => {
setTimeout(() => {
status = "on loan";
resolve();
}, 20);
});
}
async function checkLoan() {
await borrowBook();
console.log(status);
}
checkLoan();
on loan
asyncin front of a function allowsawaitinside it. You may useawaitonly inside anasyncfunction, or at the top level of a module.await borrowBook()starts the work and pausescheckLoanuntil the promise is finished. Callingresolve()is what finishes it.- The next line runs after that, so it sees the new status.
The language guarantees this order. The code after an await never runs before its promise has finished.
What this means in a Playwright test
Your test code and the browser are two separate programs. Every action is a message to the browser and an answer back, so every action returns a promise.
import { test, expect } from '@playwright/test';
test('a reader can borrow a book', async ({ page }) => {
await page.goto('/books');
await page.getByRole('button', { name: 'Borrow' }).click();
await expect(page.getByRole('status')).toHaveText('On loan');
});
The address /books is a relative path. Playwright puts the baseURL from its configuration file in front of it.
Three habits are enough for now.
- Write
asyncon the test callback. - Write
awaiton every line that does something in the browser:goto,click,fill, and eachexpecton something from the page. getByRoleon its own needs noawait. It only describes which element you mean. Nothing happens in the browser until you act on it.
When you forget an await, there is often no error. The test moves on before the browser has finished. It can then end before its check has run, and a check that never runs cannot fail. Teams find such tests months later, when the feature breaks and the test stays green.
Selenium's JavaScript library uses await in the same way. Cypress does not. Its commands are placed in a queue and run later, so you do not write await in front of them.
This lecture gives you the rule, which is enough to write and read your first tests. It does not explain how a promise works inside, how an error in asynchronous code reaches the test runner, or how the host decides what runs next. Those are separate subjects, and the Intermediate track teaches them in that order. Start with Synchronous and asynchronous code.
Remember this
A call that starts slow work returns a promise at once. await pauses your function until that promise has finished. In a Playwright test, put async on the callback and await on every action and every check of the page.
Reference: async function on MDN.