Master async/await

Almost everything new in Javascript is built on Promises and async functions, and even the existing features of the language are starting to migrate to the new structure.

But they are not just keywords that magically make something async work with the rest of the program. It changes how the function work and that can lead to errors without proper understanding.

From this book, you'll learn the basics of async/await and you'll be familiar with the common problems with it and their solutions.

Want a sneak peek? Sign up for free chapters here:


Javascript has no multithreading and synchronous waiting.

Instead, it has Promises and async/await.

Javascript started as a scripting language with the aim to make things as easy as possible. This resulted in its single-threaded model that has consequences for asynchronous operations. Without the ability to wait for a later result, callbacks became ubiquitous in frontend programming.

But callbacks are the past now as they lead to a cluttered program structure. Instead, Promises and now async/await is the mainstream way to handle asynchronous results.


Async functions are everywhere

When a function provides its result later, it has to be an async function. and every function that calls it needs to be async too. Modern Javascript programs are mostly made of async functions.

The async/await addition to the language makes using async functions a lot easier. You no longer need to interpret complicated Promise chains and use an unfamiliar error handling scheme. The complexity is hidden behind two keywords.


From the first steps to mastery

This book helps you understand how async functions works and how to use them. And with the more advanced patterns, no async/await problem will be beyond you. You'll learn:

  • How to use async functions
  • How Promises work and how they relate to async/await
  • The intricate workings of asynchronicity in Javascript and common problems

...and more!


Screenshots

Table of contents

Getting started with async/await
 Async functions
  How to use the Promise
  The benefits of Promises
  The await keyword
  Async function examples
  Chaining Promises
 Promises
  Callbacks
  The Promise constructor
  Promise states
  Result value
 Error handling
  Error callback in the Promise constructor
   Rejecting a Promise
   Detecting errors
  Error propagation in Promise chains

Parallel and sequential processing
 Parallel processing with Promise.all
  Results
  Errors in Promise.all
 Early init

Returning multiple values

Convert between Promises and callbacks
 Callback styles
  Node-style callbacks
 Convert callbacks to Promises
  Promise constructor
   Promisified functions
  util.promisify
  Usual problems
   Handle this
   function.length
 Convert Promises to callbacks

Promise timeouts
 Promise.race
 Timeout implementation
  Clear timeout
  Error object

The async serializer pattern
 Why await is not a solution
 A general-purpose solution

The async disposer pattern
 Disposer pattern

Retrying async operations
 Backoff algorithm
  Exponential backoff
 Javascript implementation
  Rejection-based retrying
  Progress-based retrying

Paginating with async generators
 Pagination
 A solution with async generators
  Breaking it down
 Making it generic

Using async functions with postMessage
 Request-response communication
  Response identification
   MessageChannel
  Error handling
 Using Promises

Collection processing with async functions
 Async for iteration
 Async functions with reduce
  Asynchronous reduce
  Timing
   await memo last
   await memo first
   When parallelism matters
 Async functions with map
  Concurrency
   Batch processing
   Parallel processing
   Sequential processing
 Async functions with forEach
  Controlling the timing
   Waiting for finish
   Sequential processing
 Async functions with filter
  Async filter with map
   Concurrency
  Async filter with reduce
   Sequential processing
 Async functions with some/every
  Using an async filter
   Short-circuiting
  Async some
  Async every
  Parallel processing

Common errors
 Not propagating errors
 Missing await in try..catch
 Not checking the return value
 Not closing resources in case of a rejection
 Using both the rejection and resolve handlers in one then function
 Not waiting for an async forEach

Glossary
About the author


About the author
Tamás Sallai

I'm a software developer focusing mostly on cloud computing and web technologies. I'm especially interested in how to handle edge cases to end up with dependable software.

One of my main focus is security and how each part affects the whole system. I'm an AWS-certified security specialist.


Get the book

The book is available from these stores: