What Is async/await?
In the previous section, we learned what a promise is and went through an exercise to see how to use it when fetching data from a server. Promises allow us to easily handle asynchronous operations. Since ECMAScript 2017, async
/await
has been added, and it provides a new way to write asynchronous code. However, async
/await
is not a completely new feature; rather it is a syntax sugar on top of promises, and it makes asynchronous code easier to read and write.
Note
async
/await
is not supported in Internet Explorer and older browsers, so please use it with caution.
As the name async
/await
suggests, it consists of two keywords, async and await. Let's talk about the async
function first.
async
The async
function helps us to write promise-based code in a synchronous fashion but without blocking the execution thread. The rest of the code runs in parallel along with its execution.
The async
keyword is added before the function, and that means the...