Playwright + TS · Lesson 8 of 36

0/36 done

Module 8: Async Programming

8. Promises, Async/Await & Promise.all()

14 minlesson

Your task

Use await Promise.all([...]) to execute loadUsers() and loadOrders() concurrently and return the results array.

Write it in the editor, then press Check answer (Ctrl + Enter).

Why Async/Await is Essential in Playwright

Browser actions take real time. JavaScript uses Promises with three states:

  • Pending: Operation in progress.
  • Fulfilled: Operation succeeded.
  • Rejected: Operation failed.
typescript
// Sequential execution: steps run one after another
async function runLoginSteps() {
  await fillUsername();
  await fillPassword();
  await clickSubmit();
}

// Parallel execution: operations happen simultaneously!
async function loadDashboardData() {
  const [profile, notifications] = await Promise.all([
    fetchUserProfile(),
    fetchNotifications(),
  ]);
}

Key Takeaways & Walkthrough Summary

Asynchronous programming is the single most important concept to master for Playwright automation. Browsers are asynchronous by nature, and every Playwright action returns a Promise. In this lesson, we will demystify Promises, async/await, and Promise.all so you never get caught by unhandled promises or race conditions. Let's master async code!

Part of the Playwright + TS course (Playwright with TypeScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

When should I use Promise.all in Playwright?

When two operations must occur concurrently, like triggering a popup click while simultaneously waiting for the popup event: Promise.all([page.waitForEvent('popup'), button.click()]).

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

TypeScript
Skip