lecture 3 of 140 completed

Promises, and why Playwright is asynchronous

The browser is a separate program, so every Playwright call returns a promise. Learn its three states.

What you'll learn

  • What a promise is and the three states it can be in
  • How then and catch receive the result or the error
  • Why every Playwright action returns a promise

A new tester prints the result of a Playwright call to see what it holds. The console shows Promise { <pending> } where they expected the text of a heading.

Your test and the browser are two separate programs. A call like page.title() sends a message to the browser and needs the answer back. That takes time, so the call cannot return the title. It returns a promise. A promise is an object that stands for a result that is not ready yet.

Here is a function that works the same way, without a browser:

function readTitle() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve("Shelf: your loans");
    }, 20);
  });
}

const result = readTitle();
console.log(result instanceof Promise);

result.then((title) => {
  console.log("title: " + title);
});
console.log("asked for the title");
true
asked for the title
title: Shelf: your loans
  1. new Promise(...) creates a promise. The function you give it receives resolve, which you call with the result when the work is done.
  2. readTitle() returns the promise at once. instanceof Promise confirms that result is a promise and not a string.
  3. .then(callback) says what to do with the result. The callback runs later, when the promise has its value.
  4. The last line runs before the callback, because the result is not ready yet.

Three states

A promise is always in one of three states.

  • pending: the work is not finished.
  • fulfilled: the work finished and there is a result.
  • rejected: the work failed and there is an error.

A promise changes state once. A fulfilled promise never becomes rejected later, and its result never changes.

The second argument of the function you give to new Promise is reject. Calling it puts the promise in the rejected state. .catch(callback) receives the error.

function borrow(title) {
  return new Promise((resolve, reject) => {
    if (title === "") {
      reject(new Error("A title is required"));
    } else {
      resolve(title + " is on loan");
    }
  });
}

borrow("Dune")
  .then((message) => console.log(message))
  .catch((error) => console.log("failed: " + error.message));

borrow("")
  .then((message) => console.log(message))
  .catch((error) => console.log("failed: " + error.message));
Dune is on loan
failed: A title is required

For the first call, then runs and catch is skipped. For the second, then is skipped and catch runs. The language guarantees which callback runs: then for a fulfilled promise and catch for a rejected one.

Do not read a rule about order into this output. The two lines come from two separate promises. The order between callbacks on separate promises is not simply the order you wrote them, and the lecture on the event loop tells you not to predict it.

A callback can never run early

Even a promise that is already fulfilled does not call then at once.

const ready = Promise.resolve("ready");

ready.then((value) => console.log("then: " + value));
console.log("after then");
after then
then: ready

Promise.resolve("ready") creates a promise that is fulfilled from the start. Its callback still waits until the current code has finished. The language guarantees this. A then callback is never called in the middle of the code that added it, so you never have to wonder whether it ran before or after the next line.

Back to Playwright

Every Playwright method that touches the browser returns a promise: goto, click, fill, textContent, title and the rest. A check on the page returns one too.

A locator is the exception. page.getByRole('button', { name: 'Borrow' }) returns a plain object that describes an element. It sends nothing to the browser, so there is no promise and nothing to wait for.

You will rarely write .then in a test. The next lecture shows await, which reads a promise's result in a way that looks like ordinary code. Under it, every await is a then.

Remember this

A promise stands for a result that is not ready yet. It is pending, then fulfilled or rejected, and it changes once. then and catch callbacks always run after the current code. Every Playwright action returns a promise because the browser is another program.

Reference: Using promises in the MDN JavaScript Guide.

now prove it

Reading is the setup. Solve this problem in the editor. We break the app on purpose to check your test would catch it.