lecture 9 of 120 completed

Promise types and the missing await

The type Promise<Member> is a promise for a member. The compiler reports some missing awaits for you and says nothing about others.

What you'll learn

  • How to read a promise type, and what await does to it
  • Which missing awaits the compiler reports
  • Which missing await it cannot see, and what finds that one

The JavaScript track spent a whole lecture on the missing await. In JavaScript you find it by reading every line. In TypeScript the compiler finds some of them.

interface Member {
  name: string;
  lateFees: number;
}

function findMember(id: number): Promise<Member> {
  return Promise.resolve({ name: "Ana", lateFees: 4 });
}

function printName(): void {
  const member = findMember(17);
  console.log(member.name);
}
members.ts(12,22): error TS2339: Property 'name' does not exist on type 'Promise<Member>'.

findMember does not return a member. It returns a promise, and a promise has no name. The compiler knows the difference because the return type says so.

Reading a promise type

Promise<Member> reads "a promise that will give a Member". The type between the angle brackets is the type of the value you get when the work is done.

await takes that value out. With a type, you can see it happen.

interface Member {
  name: string;
  lateFees: number;
}

function findMember(id: number): Promise<Member> {
  return Promise.resolve({ name: "Ana", lateFees: 4 });
}

async function printName(): Promise<void> {
  const pending = findMember(17);
  const member = await pending;
  console.log(member.name);
}

printName();
Ana

pending has the type Promise<Member>. member has the type Member. That one word is the whole difference, and in an editor you can hover over each name to see it.

An async function always returns a promise

You learned in the JavaScript track that an async function returns a promise. The return type has to say that too.

async function countLoans(): number {
  return 3;
}
loans.ts(1,30): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<number>'?

Inside the function you return a plain 3. The caller receives a promise for it, so the return type is Promise<number>. A function that returns nothing is Promise<void>.

A promise in a condition

Here is a missing await that passes for the wrong reason in JavaScript.

async function isOverdue(book: string): Promise<boolean> {
  return book === "Dune";
}

async function check(): Promise<void> {
  if (isOverdue("Emma")) {
    console.log("Send a reminder");
  }
}
loans.ts(6,7): error TS2801: This condition will always return true since this 'Promise<boolean>' is always defined.

A promise is an object, and an object counts as true in an if. Without the compiler, this code sends a reminder for every book. With await in front of the call, the if tests the real answer.

The same thing in Playwright

Every Playwright call that talks to the browser returns a promise. The types say so, and this example is checked against Playwright's own types.

import type { Page } from "@playwright/test";

export async function pageTitle(page: Page): Promise<string> {
  const title: string = page.title();
  return title;
}
catalog.ts(4,9): error TS2322: Type 'Promise<string>' is not assignable to type 'string'.

The missing await the compiler cannot see

Now the limit. Look at a call whose result nobody uses.

let fee = 4;

async function clearFee(): Promise<void> {
  await Promise.resolve();
  fee = 0;
}

async function testFeeIsCleared(): Promise<void> {
  clearFee();
  console.log("fee: " + fee);
}

testFeeIsCleared();
fee: 4

The compiler reports nothing, and the check reads the old fee. clearFee() returns a promise, and the code throws that promise away. The compiler checks that values fit the places they are put. A value that is put nowhere fits.

This is the most common shape in test code: an action or an expect on its own line, with no await. Teams often catch it with a lint rule called no-floating-promises, from the typescript-eslint project. It uses the types to find every promise that nobody awaits. The compiler alone does not.

Remember this

Promise<Member> is a promise for a Member, and await gives you the Member. The compiler reports a missing await when you use the promise as if it were the value. It says nothing when you ignore the promise, so that kind still needs a lint rule or a careful reader.

Reference: Functions Which Return Promises in the TypeScript Handbook.

now prove it

Reading is the setup. Solve this exercise in the editor. We check your code with the TypeScript compiler and list each type error.