Async Await Promises
async keyword before the
function name, or before
() when using the async arrow function. An
async function always returns a promise.
async function can contain statements preceded by an
await operator. The operand of
await is a promise. At an
await expression, the execution of the
async function is paused and waits for the operand promise to resolve. The
await operator returns the promise’s resolved value. An
await operand can only be used inside an
async...await syntax allows multiple promises to be initiated and then resolved for values when required during execution of the program. As an alternate to chaining
.then() functions, it offers better maintainablity of the code and a close resemblance synchronous code.
Async Function Error Handling
async functions uses
try...catch statements for error handling. This method allows shared error handling for synchronous and asynchronous code.
Using async await syntax
Constructing one or more promises or calls without
await can allow multiple
async functions to execute simultaneously. Through this approach, a program can take advantage of concurrency, and asynchronous actions can be initiated within an
async function. Since using the
await keyword halts the execution of an
async function, each async function can be awaited once its value is required by program logic.
async...await, multiple asynchronous operations can run concurrently. If the resolved value is required for each promise initiated,
Promise.all() can be used to retrieve the resolved value, avoiding unnecessary blocking.