Skip to Main Content
Learning JavaScript Design Patterns, 2nd Edition
book

Learning JavaScript Design Patterns, 2nd Edition

by Addy Osmani
April 2023
Intermediate to advanced content levelIntermediate to advanced
298 pages
6h 44m
English
O'Reilly Media, Inc.
Book available
Content preview from Learning JavaScript Design Patterns, 2nd Edition

Chapter 9. Asynchronous Programming Patterns

Asynchronous JavaScript programming allows you to execute long-running tasks in the background while allowing the browser to respond to events and run other code to handle these events. Asynchronous programming is relatively new in JavaScript, and the syntax to support it was not available when the first edition of this book was published.

JavaScript concepts such as promise, async, and await make your code tidier and easy to read without blocking the main thread. async functions were introduced as part of ES7 in 2016 and are now supported on all browsers. Let’s look at some patterns that use these features to structure our application flows.

Asynchronous Programming

In JavaScript, synchronous code is executed in a blocking manner, meaning that the code is executed serially, one statement at a time. The following code can run only after the execution of the current statement has been completed. When you call a synchronous function, the code inside that function will execute from start to finish before the control returns to the caller.

On the other hand, asynchronous code is executed in a nonblocking manner, meaning that the JavaScript engine can switch to execute this code in the background while the currently running code is waiting on something. When you call an asynchronous function, the code inside the function will execute in the background, and the control returns to the caller immediately.

Here is an example of synchronous ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Modern JavaScript From The Beginning

Modern JavaScript From The Beginning

Brad Traversy
Node.js Design Patterns - Third Edition

Node.js Design Patterns - Third Edition

Mario Casciaro, Luciano Mammino

Publisher Resources

ISBN: 9781098139865Errata PageSupplemental Content