Promise, async/await and a forgotten await
Without await a promise runs in the background and a test can pass without verifying anything. An async function always returns a Promise, and await is needed for everything that returns one, including locator actions and web-first assertions. The compiler catches only some cases, so the no-floating-promises and missing-playwright-await rules are enabled, and inside try you write return await.
TypeScript · Async
The test is green in 40 ms and nobody clicked the button. Where did the await go?
A test «transfer is created» passes in a few dozen milliseconds. The trace shows the click started but the test had already finished, and sometimes the run fails with «Target page, context or browser has been closed». The code looks fine and there are no type errors. The cause is two missing await words. This lesson explains what happens without them, where await is required and where it is not, and how to catch the mistake automatically.
How it works and why. An async function always returns a Promise: even with return 5 inside, from outside it is Promise<number>, and a throw becomes a rejected promise. Calling it runs the function synchronously up to the first await and immediately returns the promise. await does two things: it suspends this function until the promise settles and hands control back to the event loop, then either gives the value or throws the error. Without await nothing is suspended: the line runs, the promise goes to the background, the next line starts at once. The browser action is already sent, but the test neither waits for it nor sees its error.
A test without await ends before the action
Example: from a falsely green test to a real check. The starting code:
test('transfer is created', async ({ page }) => {
page.getByRole('button', { name: 'Send' }).click();
expect(page.getByText('Transfer sent')).toBeVisible();
});
// both calls return a Promise that nobody awaits
There are two problems. The click may not finish before the test ends, and expect(locator).toBeVisible() returns a promise: if nobody awaits it, the check guarantees nothing and the test may pass without it. The fix:
test('transfer is created', async ({ page }) => {
await page.getByRole('button', { name: 'Send' }).click();
await expect(page.getByText('Transfer sent')).toBeVisible();
});
The rule for where await is needed: everything that returns a Promise. In Playwright those are locator and page actions (click, fill, goto), reads from the browser (textContent, isVisible, count), web-first assertions (toBeVisible, toHaveText), expect.poll, expect(promise).resolves/rejects and any of your own async helpers. A plain expect(value).toBe(x) is synchronous: no await there.
What the compiler catches and what the linter catches. The compiler sees only part of the cases. A promise in an if condition it rejects:
// isVisible() returns Promise<boolean>
if (page.getByText('Cookies').isVisible()) {
await page.getByRole('button', { name: 'OK' }).click();
}
// TS2801: This condition will always return true since this
// 'Promise<boolean>' is always defined.
A «floating» promise as a standalone expression the compiler lets through. For that there are type-aware ESLint rules (they need a type-aware config, for example parserOptions.projectService): @typescript-eslint/no-floating-promises finds promises nobody handled, @typescript-eslint/await-thenable finds await on a value that is not a promise, and the eslint-plugin-playwright plugin has missing-playwright-await for Playwright methods in particular. Also a Promise<void> return type on Page Object methods shows that a method is asynchronous.
Limits and traps. A forgotten await does not always give a green test. If the action fails and nobody awaits the promise, you get an unhandled rejection: in plain Node (since version 15) that ends the process with code 1, while a test runner decides itself which test to blame, so the failure can show up in another test. One more trap: return without await inside try. The function returns the promise and leaves the try before it settles, so the rejection does not reach the catch of the same function:
async function loadBalance(id: string): Promise<number> {
try {
return client.getBalance(id); // no await
} catch {
return 0; // the error never gets here
}
}
// inside try you need: return await client.getBalance(id);
The @typescript-eslint/return-await rule in in-try-catch mode requires exactly this. Outside try an extra return await gives nothing.
Common mistakes.
awaiton the action only and forgotten onexpect(...).toBeVisible(): the check guarantees nothing.if (locator.isVisible())withoutawait: the condition is always true (TS2801 only withstrictNullChecks).awaitwhere the value is synchronous: harmless but noise, andawait-thenableflags it.return promiseinsidetry: the error bypassescatchandfinallyruns before the operation finishes.- A linter without a type-aware config:
no-floating-promisesdoes not work, and few people notice the config error.
Check yourself
A test has page.getByRole('button').click(); without await, and the next line is await expect(page).toHaveURL(/done/) with await. Is it safe?
Answer
No. The URL check will wait, but the click may happen after it starts or fail with an error nobody sees. The test depends on a random order of events and can be flaky. The action must be awaited just like the assertion.
Why does return client.get() inside try not reach the catch, while return await client.get() does?
Answer
Without await the function hands the promise out and leaves the try at once, and the rejection happens in the caller's code. With await the function waits inside try, so the rejection becomes an exception right there.
async function returns a promise and await waits for its result. A forgotten await in a test means the action or assertion is not waited for, so the test may pass without checking anything. Middle/Senior: Without await the promise goes to the background, the test ends with its body, and the rejection becomes an unhandled rejection in another test. So I enable no-floating-promises and missing-playwright-await with a type-aware config, and write return await inside try. The compiler catches only some cases, such as a promise in a condition, so it cannot be covered without a linter.