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:
- Is every promise awaited, returned, or deliberately handed off? A promise nobody holds is a failure nobody hears about.
- Does this callback's caller wait for promises?
forEach,filter,mapwithoutPromise.all, event emitters and constructors don't. - Is a promise used as a value? In an
if, afilteror aBoolean(…), a promise is always truthy. - Sequential or parallel? Independent awaits in a row are slow; dependent ones (a cursor, an ordering) must stay in a row.
- What happens when one of many fails?
allfails fast,allSettlednever fails,anyneeds one success,racetakes the first to settle. - How many run at once?
Promise.allover a user-sized list has no limit. - Is every promise handled from the moment it starts? One started early and awaited later can reject while nobody is listening.
- Does a
.thenchain return its inner promises, and is.catchat the end?
Tells
| You see | Ask |
|---|---|
.forEach(async | Who waits for these? |
.filter(async / .some(async | Is a promise being used as a boolean? |
A call to an async function with no await | Is the result used as if it were the value? |
Several awaits in a row | Do they depend on each other? |
Promise.all(list.map( | What if one fails? How long is the list? |
allSettled | Does anything look at status? |
this.something() in a constructor | Is it async? Who waits? |
const p = fn() … await other … await p | Can p reject while nothing is listening? |
new Promise(async | Where do its errors go? |
Promise.race with a timer | Is the timer cleared? Is the work cancelled? |
if (!cached) cached = await … | What do callers do while the first load is in flight? |
Go deeper
- Promises in Real Code and Async/Await Traps
- Controlling Concurrency: limits, pools and backpressure, with a simulation
- Previous surface: try / catch / finally · Next: Timers