Promises & Closures

Promises & Closures: Promise Flow — pending → fulfilled/rejected Promise and Closure are JavaScript's two most fundamental concepts and both work deep inside Playwright tests.

Promise Flow — pending → fulfilled/rejected

Promise and Closure are JavaScript's two most fundamental concepts and both work deep inside Playwright tests. A Promise is like a restaurant pager: you get a number when seated, it vibrates when your food is ready — "wait, I'll notify you when done." It has three states: `pending` (being prepared), `fulfilled` (ready), `rejected` (cancelled). Why doesn't Java have this concept? Java is multi-threaded so `CompletableFuture` plays a similar role, but callback chains via `.thenApply()/.thenCompose()` are less readable than Promise chains. A Closure is like someone leaving their bag at the door but still having items in their pockets (private variables): the function has been returned, the outer scope has closed, but the inner function continues to remember the outer variables. In QA, closures are used to store test fixture data and config values across functions.

Promise & Closure Examples

// 1. Defining a Promise (Ice Cream Pager) const iceCreamPromise = new Promise((resolve, reject) => { const isReady = true; if (isReady) resolve('🍦 Ice Cream Ready!'); else reject('😢 Ice Cream Sold Out.'); }); // 2. Closure Example (Backpack / Private Variable) function createBackpack() { let toy = '🧸 Teddy Bear'; // private variable — not accessible from outside return { getToy: () => toy, setToy: (newToy) => { toy = newToy; } }; } const myBackpack = createBackpack(); console.log(myBackpack.getToy()); // '🧸 Teddy Bear'

Micro Lab: JavaScript QA coding practice

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

Step by Step: JavaScript QA coding practice

Read the goal and async dependencies

Place await / Promise chain in the right spot

Complete the assertion or expectation line

Run and read console or test output

If flaky, add a wait strategy or retry

What is the safe order for writing and testing JavaScript QA code?

🎬 The Three States of a Promise: Pending → Resolved/Rejected → then/catch