liveBook · Manning

Chapter 6: Chapter 6. Functions for the future: generators and promises

What you will learn in chapter 6 of Secrets of the JavaScript Ninja, Second Edition

Introduces generators and promises as ES6 features, explaining their basic roles before the chapter explores how to use them.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces generators and promises as ES6 features, explaining their basic roles before the chapter explores how to use them.

Key terms: generators, suspend, resume, promises, asynchronous code, placeholder

6.1 Making our async code elegant with generators and promises

The section compares blocking server requests, nonblocking callbacks, and a more elegant approach combining generators and promises.

Key terms: single-threaded execution model, blocked our UI, callbacks, generators, promises, nonblocking, boilerplate error-handling code

6.1 Do you know?

This section poses diagnostic questions about generators, promises, and Promise.race without explaining their answers.

6.2 Working with generator functions

Introduces generators as functions that produce values on request, shows their basic syntax and use in a for-of loop, and explains what calling one returns.

Key terms: generator, yield, suspended, generator function, for-of loop, iterator

6.2.1 Controlling the generator through the iterator object

This section explains how an iterator controls a generator, how loops consume its values, and how yield* delegates to another generator.

Key terms: generator, iterator object, next, yield, value, done, while loop, for-of loop, syntactic sugar, yield*

6.2.2 Using generators

Shows two practical uses of generators: producing unique IDs on demand and traversing a DOM tree as a sequence of nodes.

Key terms: generator, id, yield, iterator object, next(), DOM traversal, generator function, for-of loop, callbacks

6.2.3 Communicating with a generator

This section explains how to send values or exceptions into a generator after it has yielded, enabling two-way communication.

Key terms: yield expression, iterator, next method, generator function arguments, two-way communication, throw method, exception, try-catch block

6.2.4 Exploring generators under the hood

This section explains generator state changes and how an iterator preserves and reactivates a generator’s execution context between yields.

Key terms: Suspended start, Executing, Suspended yield, Completed, yield, next, execution context, execution context stack, iterator, ninjaIterator

6.3 Working with promises

Introduces promises as placeholders for asynchronous results and shows how to create one and handle its success or failure.

Key terms: promise, asynchronous computation, Promise constructor, executor function, resolve, reject, then, success callback, failure callback

6.3.1 Understanding the problems with simple callbacks

This section explains why simple callbacks make asynchronous work difficult and why JavaScript introduced promises as a standard approach.

Key terms: asynchronous code, long-running task, callback, try-catch, event loop, err, data, nested callbacks, pyramid of doom, interdependent

6.3.2 Diving into promises

This section explains promise states and uses immediate and delayed promises to show when their success callbacks run.

Key terms: promise, pending, unresolved promise, fulfilled, rejected, resolved, Promise constructor, executor function, resolve function, then method

6.3.3 Rejecting promises

This section explains how promises are rejected explicitly or by unhandled exceptions, and how either rejection reaches an error callback.

Key terms: reject method, executor function, implicit rejection, exception, then method, second, error, callback, catch method, rejection callback

6.3.4 Creating our first real-world promise

This section uses a JSON-fetching function to show how a promise handles asynchronous success and several kinds of failure through a single interface.

Key terms: getJSON, XMLHttpRequest, onload, onerror, status 200, JSON.parse, success and failure callbacks, error sources

6.3.5 Chaining promises

This section explains how chaining promises expresses dependent asynchronous steps without deeply nested callbacks and how one catch can handle failures across the chain.

Key terms: then, promise chaining, pyramid of doom, getJSON, catch, error callback, chained promises

6.3.6 Waiting for a number of promises

This section explains how Promise.all waits for multiple independent asynchronous tasks and how Promise.race responds to the first one that settles.

Key terms: Promise.all, array of promises, succeed values, Promise.race, resolves, rejects

6.4 Combining generators and promises

The section shows how a generator and promises can coordinate dependent asynchronous requests without blocking, while keeping the business logic readable.

Key terms: generator, yield, promise, iterator, async function, handle function, next, throw, getJSON, control-flow

6.4.1 Looking forward—the async function

This section introduces async and await as planned language support that makes asynchronous code easier to write without the generator-and-promise boilerplate.

Key terms: async, await, generators, promises, boilerplate code, async functions, transpilers, Babel, Traceur

6.5 Summary

6.6 Exercises

Where this is used later

Concepts explained in this chapter

← Chapter 5: Chapter 5. Functions for the master: closures and scopes Chapter 7: Chapter 7. Object orientation with prototypes →

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.