8 min read

Eye Training · Promises and async/await

Every promise is a question: who waits for it, and who hears if it fails?

Part 2 · Surface 12. Bug counts are hidden and some snippets are clean. See Start Here for how to use these drills.


J · The basics

Bug hunt
J · Send reminders
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Can this user edit?
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Load the dashboard
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Fetch every page
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Active users
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min

M · Three years in, these should jump out

Bug hunt
M · Refresh feeds
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Count the successes
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Resize every image
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Get the price
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
M · Publish a post
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Search index
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
M · Load an order
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min

S · Where seniors stand out

Bug hunt
S · Load a profile page
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
S · Wrap in a promise
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
S · Add a timeout
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
S · Load settings once
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min

What your eye checks

Whenever you see a promise, async or await, ask:

  1. Is every promise awaited, returned, or deliberately handed off? A promise nobody holds is a failure nobody hears about.
  2. Does this callback's caller wait for promises? forEach, filter, map without Promise.all, event emitters and constructors don't.
  3. Is a promise used as a value? In an if, a filter or a Boolean(…), a promise is always truthy.
  4. Sequential or parallel? Independent awaits in a row are slow; dependent ones (a cursor, an ordering) must stay in a row.
  5. What happens when one of many fails? all fails fast, allSettled never fails, any needs one success, race takes the first to settle.
  6. How many run at once? Promise.all over a user-sized list has no limit.
  7. Is every promise handled from the moment it starts? One started early and awaited later can reject while nobody is listening.
  8. Does a .then chain return its inner promises, and is .catch at the end?

Tells

You seeAsk
.forEach(asyncWho waits for these?
.filter(async / .some(asyncIs a promise being used as a boolean?
A call to an async function with no awaitIs the result used as if it were the value?
Several awaits in a rowDo they depend on each other?
Promise.all(list.map(What if one fails? How long is the list?
allSettledDoes anything look at status?
this.something() in a constructorIs it async? Who waits?
const p = fn() … await other … await pCan p reject while nothing is listening?
new Promise(asyncWhere do its errors go?
Promise.race with a timerIs the timer cleared? Is the work cancelled?
if (!cached) cached = await …What do callers do while the first load is in flight?

Go deeper