Skip to content

Async Patterns Deep Dive

Master callback patterns, Promises, async/await, and concurrent async coordination.

What you'll learn

  • Callback hell and its solutions
  • Promise chaining and composition
  • async/await error handling
  • Concurrent async: Promise.all, race, allSettled

Concept

Async Patterns Deep Dive

Asynchronous programming handles operations that complete in the future - I/O, network requests, timers. JavaScript event loop makes async natural, but the patterns have evolved from callbacks to Promises to async/await.

Callbacks

The original async pattern: pass a function to be called when the operation completes. Callback hell (pyramid of doom) happens when you nest callbacks: doA(() => doB(() => doC(() => ...))). The code reads inside-out and error handling is duplicated at every level.

Promises

A Promise represents a future value. It can be pending, fulfilled, or rejected. Promises solve callback hell through chaining: doA().then(doB).then(doC).catch(handleError). Errors propagate automatically through the chain. Promises are values - you can store them, pass them, and compose them.

async/await

Syntactic sugar over Promises that makes async code look synchronous. const data = await fetchData() pauses the function until the Promise resolves. Error handling uses try/catch. This is the most readable form, but remember: the function still yields to the event loop at each await.

Concurrent Coordination

Promise.all([p1, p2, p3]): wait for all, fail-fast on first rejection. Promise.allSettled: wait for all, never fail. Promise.race: resolve/reject with the first to settle. Promise.any: resolve with the first to fulfill.

Sequential vs Concurrent

for (const url of urls) { await fetch(url); } is sequential. await Promise.all(urls.map(fetch)) is concurrent. For large lists, use a concurrency limiter.

Example

              const delay = (ms, val) => new Promise(r => setTimeout(() => r(val), ms));
async function sequential() {
  const start = Date.now();
  const a = await delay(100, 'a'); const b = await delay(100, 'b'); const c = await delay(100, 'c');
  return {a, b, c, time: Date.now() - start};
}
async function concurrent() {
  const start = Date.now();
  const [a, b, c] = await Promise.all([delay(100, 'a'), delay(100, 'b'), delay(100, 'c')]);
  return {a, b, c, time: Date.now() - start};
}
async function fetchAll(urls) {
  const results = await Promise.allSettled(urls.map(u => u === 'bad' ? Promise.reject('Failed') : Promise.resolve('OK: ' + u)));
  return results.map(r => r.status === 'fulfilled' ? r.value : r.reason);
}
sequential().then(r => console.log('Sequential:', r.time, 'ms'));
concurrent().then(r => console.log('Concurrent:', r.time, 'ms'));
fetchAll(['a', 'bad', 'c']).then(r => console.log('Results:', r));
            

Sequential awaits one after another (total ~300ms). Concurrent uses Promise.all to start all at once (~100ms). allSettled collects results even if some fail.

Try it

  • Hash Generator

    Hash generation can be async - see how Promise patterns apply to computation.

Common mistakes

The mistake

Awaiting in a map callback, losing concurrency.

The fix

urls.map(async url => await fetch(url)) returns an array of Promises. You need Promise.all() to wait for them.

The mistake

Using Promise.all when one failure should not stop others.

The fix

Promise.all rejects on the first failure. If you need all results regardless of individual failures, use Promise.allSettled.

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors