Manuals / JavaScript / Chapter 7

C · Async · intermediate · Week 4–5 · Chapter 7 of 14

Promises & async/await

JavaScript is single-threaded but non-blocking. Promises represent future values. async/await is syntactic sugar over Promises — readable sequential async code. Promise.all for parallel, Promise.race for first-wins. Always handle rejections.

Path progress
44%

Step 1 of 5

Promise basics

new Promise((resolve, reject) => ...) wraps async work. .then handles success, .catch handles failure. A Promise is pending → fulfilled or rejected once.

Promise basicsDrag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Promise basicsdrag · tap →Try thisdrag · tap →Follow the dashed drag · tap →

Example

function delay(ms) {
  return new Promise(resolve => setTimeout(resolve, ms))
}

delay(1000).then(() => console.log("done"))

Do this now

Wrap setTimeout in a delay(ms) function returning a Promise. Chain .then to print "done" after 1 second.

Was this step clear?
Chapter learning outcomes
  • Callback → Promise mental model
  • then/catch/finally
  • async/await
  • Promise.all / Promise.allSettled
  • Error propagation
Chapter videos

Traversy Media

Async JS — callbacks, promises, await

Waiting correctly is half of reliable automation.

Open on YouTube →

Clear these before you leave

Side quest

Async pipeline

Load user IDs from a JSON file, fetch each user from JSONPlaceholder in parallel (max 3 concurrent), aggregate results.