Chapter 1

Asynchronous APIs

Many browser APIs are asynchronous in nature. This chapter contains recipes for performing basic tasks asynchronously using callback functions, events, and Promises.


Recipe 1.1
Working With Promises

Load some data using a Promise-based API.

Recipe 1.2
Loading an Image With a Fallback

Load and show an image. In case of error, show a fallback image.

Recipe 1.3
Chaining Promises

Call a sequence of Promise APIs, where each depends on the result of the previous.

Recipe 1.4
Using the async and await Keywords

Load some data using a Promise-based API, and wait for the Promise with the await keyword.

Recipe 1.5
Using Promises in Parallel

Use Promise.all to run several asynchronous operations in parallel.

Recipe 1.6
Animating an Element with requestAnimationFrame

Asynchronously animate an element using the requestAnimationFrame API.

Recipe 1.7
Wrapping an Event API in a Promise

Create a Promise wrapper around an event-based API.

Web Browser API Cookbook
Joe Attardi