lecture 6 of 150 completed

Arrow functions and callbacks

test('name', () => { ... }) passes a function to a function. Every test file is written this way.

What you'll learn

  • What a callback is
  • The difference between passing a function and calling it
  • How to read and write an arrow function

Here is a very small test runner. It prints the name of a test and then runs it. The call on the last line looks reasonable, and it breaks.

function runTest(name, body) {
  console.log("running: " + name);
  body();
}

function checkLoan() {
  console.log("checking the loan");
}

runTest("loan", checkLoan());
checking the loan
running: loan
TypeError: body is not a function

The check ran before the runner started. Then the runner failed.

The cause is the pair of brackets after checkLoan. checkLoan() calls the function right there, on that line. It prints its message and gives back undefined. So runTest receives undefined as its body, and undefined cannot be called. The language guarantees the TypeError. The sentence after it is how Chrome and Node word it.

Without the brackets, checkLoan is the function itself. You can hand a function to another function the same way you hand over a number.

function runTest(name, body) {
  console.log("running: " + name);
  body();
}

function checkLoan() {
  console.log("checking the loan");
}

runTest("loan", checkLoan);
running: loan
checking the loan

A function that you pass to another function is called a callback. You hand it over, and the other function calls it when it is ready.

Arrow functions

Naming every callback takes space. An arrow function lets you write the function where you use it. It has a parameter list, the arrow =>, and a body.

function runTest(name, body) {
  console.log("running: " + name);
  body();
}

runTest("loan", () => {
  console.log("checking the loan");
});
running: loan
checking the loan

() => { ... } is a function with no parameters. Nothing inside it runs until runTest calls body().

Now this line from a Playwright test file should read differently:

import { test } from '@playwright/test';

test('a reader can borrow a book', async ({ page }) => {
  // the steps of the test go here
});

The first line brings test in from the Playwright package. The modules lecture explains that line.

test is a function. You call it with two arguments, a name and a callback. The test runner keeps the callback and calls it later. That is why the steps do not run at the moment the file is loaded. The words async and { page } have their own lectures later in this track.

The short form returns, the long form does not

An arrow function without curly brackets returns its one expression. With curly brackets, you must write return yourself.

const double = (n) => n * 2;
const broken = (n) => { n * 2; };

console.log(double(4));
console.log(broken(4));
8
undefined

The second one is the missing return from the last lecture, in a new shape. It appears often in the callbacks you pass to array methods, which come later in this track.

Remember this

name is the function. name() runs it and gives you its result. A callback is a function you pass for someone else to call. An arrow function with curly brackets needs its own return.

Reference: Arrow function expressions on MDN.

now prove it

Reading is the setup. Solve this exercise in the editor. We run your code on each case and compare what it prints with the expected output.