Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Typing Async Functions

In this lesson, you gave the club’s species lookups promise types, watched the checker catch a missing await in a total an AI assistant suggested, and looked up a day’s sightings all at once with Promise.all().

Key Points

  • An async function’s return type is a promise. countIfKnown() returns a sighting’s count, a number, but an async function always returns a promise, so you annotate its return as Promise<number>. The number is what the promise gives you once it fulfills. The checker could infer this type on its own, but writing it down tells callers what they get and has the checker hold the function’s body to it.
  • await turns a Promise<number> back into a number. It waits for the promise to fulfill and gives you what’s inside, so the checker knows the type on each side of every await.
  • A promise where the types need its value is a type error. The suggested total += countIfKnown(sighting) adds a promise to a number, and npm run check reports it before anything runs: error TS2365, “Operator ‘+=’ cannot be applied to types ‘number’ and ‘Promise‘“, followed by “Did you forget to use ‘await’?” Adding await before the call fixes it.
  • Node.js runs the unchecked file anyway. It doesn’t check types, so the helper’s version prints Known birds: 0 followed by [object Promise] four times. With a promise, += turns both sides into text, so each promise gets glued on.
  • Promise.all() keeps the element type. today.map(countIfKnown) is a Promise<number>[]. Read from the outside in, the [] comes last, so it’s an array of promises. Promise.all() turns it into one Promise<number[]>, a single promise that holds an array of numbers, and awaiting that gives a number[], one count per sighting, in the same order.
  • The checker doesn’t catch every forgotten await. A promise dropped into a template literal without await gets no error, and neither does a call whose promise you never use at all. A linter rule called no-floating-promises catches the unused ones.

Final Code

Here’s daytotal.ts at the end of the lesson, run with the species API started by npm run api. The Sighting type is cut down from what the app exports, and "Atlantic Pufin" is misspelled, so the API doesn’t know it. It answers an unknown name with a not-found status, so response.ok is false and the count is zero:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 The species lookups are async, so in this lesson, you’ll give them types. You’ll see what await does to a type, and how the checker uses that.

00:10 Here are four of today’s sightings. The sighting type is cut down from what the app exports. The club counts only birds the species API knows. With the API running, the function countIfKnown asks the API about one sighting.

00:25 If the API knows the species, it returns the sighting’s count. A name it doesn’t know gets a not found status, so the response isn’t ok, and the count is zero.

00:36 That return statement gives a number. But an async function always returns a promise, so you annotate its return type as a Promise of number, after the parameters.

00:46 The number is what the promise gives you once it fulfills. The checker could work this type out on its own. Writing it down tells callers what they get, and the checker holds the function’s body to it.

01:00 Now the club wants a day’s total. An AI assistant suggested a function for it, knownTotal, that starts at zero, loops over the sightings, and adds each sighting’s count.

01:11 It looks fine. Your turn: pause the video and predict. Does this pass the type check?

01:22 It doesn’t. Run npm run check, and the checker reports error TS2365 at line 20. Read it from the top. The += operator can’t be applied to a number and a Promise of number.

01:37 The number is total. The Promise of number is what calling countIfKnown gives you. Nothing awaits it here, so the promise itself goes into the sum. The last part asks: did you forget to use await?

01:51 Node doesn’t check types, so it runs this file anyway. The total comes out as zero, followed by [object Promise] four times. += with a promise turns both sides into text, so each promise gets glued on.

02:08 Calling countIfKnown gives a Promise of number. Await waits for that promise to fulfill, and gives you what’s inside, a number. So add await before the call.

02:19 Now the right side is a number, and a number plus a number is fine.

02:24 Check again, and the checker reports no errors. Run it, and the day’s known birds total seven: two red kites, one little egret, and four curlews. The misspelled puffin counts zero.

02:39 The total hides which sighting the API didn’t know. To see each count, start all four lookups at once. Map the sightings through countIfKnown, and lookups is an array of promises of number.

02:52 Read it from the outside in. The square brackets come last, so the outer layer is the array. Promise.all takes that array. The start of its hover names the method and what this call passed in.

03:04 The end says it returns one promise, and that promise holds an array of numbers. This time, the square brackets sit inside the angle brackets. Await it, and counts is an array of numbers, one count per sighting, in the same order.

03:20 Run the file, and counts shows two, one, zero and four. The zero is the misspelled puffin.

03:29 An async function returns a promise, so annotate its return as a Promise of its value’s type. Await turns that promise back into the value, and the checker knows the type on each side.

03:40 So when a promise lands where the types need its value, like adding it to a number, the checker reports it before anything runs. It doesn’t catch every one.

03:51 A promise dropped into a template literal without await, or a call whose promise you never use at all, gets no error. A linter rule called no-floating-promises catches the unused ones, and that’s all you need about it for now.

04:04 Your lookups have types now, but one can still fail when the API is offline. You’ll handle that in Catching Errors as unknown.

Become a Member to join the conversation.