Async/Await
Async/await is a new syntax form added to simplify code that uses promises. Async/await introduces two new keywords: async
and await
. Async is added to function declarations and await is used inside an async
function. It is surprisingly easy to understand and use. In its simplest form, async/await allows us to write promise-based asynchronous code that looks almost identical to the synchronous code that does the same task. We will use async/await to simplify code using promises and make it even easier to read and understand.
Async/Await Syntax
The async
keyword is added to function declarations; it must precede the function keyword. An async
function declaration defines an asynchronous function. An example declaration of an async
function is shown in the following snippet:
async function asyncExample( /* arguments */ ){ /* do work */ }
Snippet 2.34: Implementing promises
An async
function implicitly returns a promise, no matter what the return value is specified to be. If the return...