O'Reilly logo

JavaScript: The Definitive Guide, 7th Edition by David Flanagan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 8. Asynchronous JavaScript

Some computer programs, such as scientific simulations and machine learning models are compute-bound: they run continuously, without pause, until they have computed their result. Most real-world computer programs, however, are significantly asynchronous. This means that they often have to stop computing while waiting for data to arrive or for some event to occur. JavaScript programs in a web browser are typically event-driven, meaning that they wait for the user to click or tap before they actually do anything. And JavaScript-based servers typically wait for client requests to arrive over the network before they do anything.

This kind of asynchronous programming is commonplace in JavaScript and this chapter documents three important language features that help make it easier to work with asynchronous code. Promises, new in ECMAScript 6, are objects that represent the not-yet-available result of an asynchronous operation. The keywords async and await were introduced in ECMAScript 2017 and provide new syntax that simplifies asynchronous programming by allowing you to structure your Promise-based code as if it was synchronous. Finally, asynchronous iterators and the for/await loop were introduced in ECMAScript 2018 and allow you to work with streams of asynchronous events as if they were simple synchronous loops.

Ironically, even though JavaScript provides these powerful features for working with asynchronous code, there are no features of the core ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required