5. Mastering Promises & Async/Await

Why Playwright Operations Return Promises

When you command page.click("#submit"), Node sends an instruction to the browser via WebSocket, the browser waits for element stability, and responds. Therefore, every Playwright action returns a Promise!
// Sequential execution:
async function completeCheckout(page) {
  await page.click("#cart");
  await page.fill("#coupon", "DISCOUNT20");
  await page.click("#apply-btn");
}

// Parallel execution with Promise.all:
async function loadTestData() {
const [users, products] = await Promise.all([
fetchUsersApi(),
fetchProductsApi()
]);
return { users, products };
}

Task

  • Implement runParallelTasks:
  • 1. Accept array of async task functions.
  • 2. Execute all tasks in parallel using Promise.all and return array of results.
πŸ’‘Common doubts
Did you like the problem?
Lesson 5 of 35
JavaScript
Loading code editor…
Next