Member-only story
Async and Await in javascript
Async and await are extension of promises in javascript. if you are not clear about promises, please go through my other tutorial.
async can be declared with function only and await can be used inside async function only.
From Mozilla website
async function
declaration defines an asynchronous function — a function that is an AsyncFunction
object. Asynchronous functions operate in a separate order than the rest of the code via the event loop, returning an implicit Promise
as its result. But the syntax and structure of your code using async functions is much more like using standard synchronous functions.
So Basically
- The function operates asynchronously via event loop.
- It uses an implicit Promise to return the result.
- The syntax and structure of the code is similar to writing synchronous functions.
Further Mozilla document says
An async
function can contain an await
expression that pauses the execution of the async function and waits for the passed Promise
's resolution, and then resumes the async
function's execution and returns the resolved value. Remember, the await
keyword is only valid inside…