Human brains are not designed to handle asynchrony efficiently. The async/await functions allow us to write code that looks synchronized but executes it asynchronously.
Why to use async/await functions?
- We can write clean and concise code with a few errors using async/await. This will increase the readability of nested and complex code.
- Helps in Debugging. Errors can be handled in one place by using try/catch.
- The async/await functions have a proper error stack, unlike the ambiguous stack received by Promise, which makes it difficult to find where the error occurred.
Async is a keyword that can be used for the declaration of synchronous functions. Through the async function, you can make sure that you don’t break the thread of execution, which can be written like synchronizing promise-based code.
Features of async Function
- Works asynchronously through an event loop
- Always returns a value
Syntax of async Function
The syntax of the async function is as follows:
Here, the name represents the function name, params represent the name of parameters to be passed in the function, and statements contain the body of the function and one or more await expressions used within the async function.
A Simple Example of async Function
var data = "Hello World";
getData().then(data => console.log(data));
Let’s take a look at another example:
The above function is equivalent to the following function:
return Promise.resolve(1).then(() => undefined)
Above example represents that the code after each await expression is likely to be in the callback i.e .then. Through the functions, the promise chain is automatically constructed step by step at each reentry stage. The return value forms the last step in the chain.
Features of Await Function
- Can only be used within asynchronous blocks.
- Makes the code wait for the Promise to return a result.
- Only asynchronous blocks will be kept waiting.
Syntax of Await Function
The syntax of await expression is as follows:
Here, await represents any value to wait for or promise, rv returns the value if there is no promise, or else returns the expected value of the promise.
A Simple Example of await Expression
var y = await "Hello World";
In the example above we can see that the console.log(1) was printed first as expected. After this the getData() function was called where we initiated a variable with the name of y and we used the await keyword in it.
The await keyword will cause the JS runtime to stop or pause the code in that line i-e where we have written “Hello world”. It won’t allow the execution of further code until the async function call returns the result. It will send this task to the task queue and the next lines of code will start to run.
In the next line after calling the function getData() we are console logging the 2 to the console window. Hence we saw 2 before “Hello world”.
Now after this when the async call function returns the result only then will the code resume to run again and hence we see “Hello World” at the end of the console window.