16.7 Async Functions
Promises are very good for structuring interdependent asynchronous operations in a readable way and avoiding nested callback functions, called “callback hell.” However, callback functions aren’t redundant compared to the previous callback-based approach. And this is where another language feature of JavaScript comes into play: async functions. Based on the concept of promises, you can write asynchronous source code without callback functions using the async and await keywords.
The await keyword lets you wait for the result of an asynchronous operation, as it were, and lets you assign the result of a successfully resolved promise to a variable. To use await, you must be in a function marked async. In Listing 16.26, this ...
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.
Read now
Unlock full access