liveBook · Manning

Chapter 8: Functions for the future: promises and generators

What you will learn in chapter 8 of Secrets of the JavaScript Ninja, Third Edition

The chapter introduces promises as a modern alternative to callbacks for dealing with future events and previews async/await and generators.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces promises as a modern alternative to callbacks for dealing with future events and previews async/await and generators.

Key terms: callback, promise, event

8.1 The power of promises

This section introduces promises as objects that represent operation outcomes and explains why separating an outcome from its callback is useful.

Key terms: promise, callback, outcome of an operation, handler, then, resolved

8.1.1 Resolving and settling promises

This section explains promise states, how promises are resolved or rejected, and why resolving a promise does not always settle it.

Key terms: pending, fulfilled, rejected, settled, executor, resolve, reject, reason, thenable, resolved

8.1.2 Chaining promises with then()

Explains how promise handlers create chains, how errors flow to catch, and how finally runs after settlement.

Key terms: then, onFulfilled, onRejected, pending, resolve, reject, catch, promise chain, fetch(), response.ok

8.1.3 Dealing with multiple promises

This section explains how to combine concurrent promises, choose among their outcomes, handle timeouts, and recognize when a concurrency-limiting utility is needed.

Key terms: Promise.all, fulfilled values, rejection reason, Promise.allSettled, settled states, Promise.any, first promise to be fulfilled, Promise.race, AbortController, signal

8.1.4 Promise timing: Of microtasks and macrotasks

Explains why promise callbacks run after synchronous code but before timeout callbacks, using the event loop’s microtask and macrotask queues.

Key terms: settled promise, then callback, event loop, microtask queue, job queue, macrotask queue, task queue, microtasks, queueMicrotask, setTimeout

8.2 Flattening promise callbacks with await

Introduces await as a simpler-looking way to work with promises than the .then() chains common in earlier JavaScript code.

Key terms: promises, .then(), await

8.2.1 The problem with nested callbacks

This section explains why nested callbacks make sequences of asynchronous operations hard to read and introduces await as a more readable way to express them.

Key terms: callbacks, callback hell, state, promises, await, promise-based APIs, event-driven single-threaded model

8.2.2 How await works

Explains how await pauses the current code without blocking other events, how it resumes or throws, and where JavaScript permits its use.

Key terms: await, Promise.resolve(value), settled, microtask, fulfilled, rejected, then, stack trace, ECMAScript module (ESM), top level

8.2.3 Automating promise creation with async

This section explains how async functions automatically return promises, turn thrown errors into rejections, and make await safe to use without interrupting the caller.

Key terms: async function, promise resolution algorithm, rejected promises, await, promise, Promise<string>, fulfilled value, rejection reason, unknown

8.3 Making interruptible loops with generators

Introduces generators as a way to define functions that return iterables and explains why their anticipated role in asynchronous code diminished.

Key terms: generators, iterable, async, promise, promises, nested callbacks, async and await

8.3.1 Iterables and iterators

This section explains the iterator protocol that lets JavaScript consume sequential values from arrays, other collections, and custom iterables.

Key terms: Symbol.iterator, iterable, iterator, next(), iterator result, value, for…of, destructuring, spread, rest

8.3.2 Iterating within a function with generators

This section explains how generator functions produce iterable generator objects that pause and resume at yields, accept values through next(), and delegate iteration with yield*.

Key terms: generator function, generator object, function*, iterable, iterator, Symbol.iterator, next(), yield, value, done

8.3.3 Async iterables and async generators

This section explains how async generators combine iteration with asynchronous waiting, how their results are consumed, and when wrapping work in an iterable is useful.

Key terms: yield, await, async generators, Symbol.asyncIterator, async iterable, async iterator, next(), promise, async, function*

8.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 7: Functions for the master: closures and scopes Chapter 9: Objects, prototypes, and classes →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.