Async and Await in Node.js | Async in IIFE

What is Async and Await in nodejs? How you can write Async function in IIFE

  Shubham Verma      March 9, 2022


Async and Await in Node.js | Async in IIFE

Async and Await in Node.js | Async in IIFE

Async and Await in nodejs are broadly used for asynchronous programming. In this article, we will learn about What is Async and Await in nodejs? How you can write the Async function in IIFE?


Why we use async and await?

Async and Await are very common in JavaScript. We use Async and Await to achieve the asynchronous flow of execution. In javascript, if you want to make a function as promisify, then you can use async, which means async make your function promise-based.

What is async and why we use async?

Async is a special keyword in javascript, this async keyword would be placed before a function definition. After placing the keyword async before a function, the function will behave differently. After placing the async keyword before a function, the function will be a promise-based function. This function will act as a promise in javascript. So this is the main task of async in javascript.
If you added async before a function, then you need to call this function as you use to call a promise function, like you can add .thenand get the result in the .then block. Internally, javascript engine convert that function into a javascript promise function. This is how we use async keyword before the function.

and you can call this function as:

or
Inside asyn function, you can call as:


Run the code:

Async and Await in Node.js | Async in IIFE

Async and Await in Node.js | Async in IIFE


What is await and why we use await?

Await is also a javascript special keyword. the keyword await describes as its name "wait", Means whenever you want to wait you will use "await" in Javascript.
Let's describe it this way, In your code, at any line/statement is a kind of promise, callback, or time taking and we want to execute the next line of code after getting this current the result of the current statement, then we place "await" before this statement.
If you place "await" before any statement then the JS engine will wait for that line to be executed and when that line finish executing then the next statement will be executed.

Rule for await?

This javascript magic keyword will work only under the async function. You can place the "await" keyword inside the async function.

What is IIFE?

IIFE stands for Immediately Invoked Function Expression. Click here to know about Immediately Invoked Function Expression (IIFE).

How to use Async in IIFE?

How we can use async in IIFE is very easy. Below are the codes that you can use async in IIFE.

Async and Await in Node.js | Async in IIFE

Async and Await in Node.js | Async in IIFE

Need other examples?


Async and Await in Node.js | Async in IIFE

Async and Await in Node.js | Async in IIFE


Pro Tip:

You can use await in API call, this is not a pro tip HAHAHA...
The pro tip is if you are using await in API call then you can use object destructuring syntax to extract the data further in Node version 5.3.0. You need to turn on the flag through the command line using
node --harmony_destructuring FILE_NAME.js
Async and Await in Node.js | Async in IIFE

Async and Await in Node.js | Async in IIFE


Conclusion:

In this article, we learn about the Async and Await in Node.js, Why we use async and await?, What is async and why we use async?, What is await and why we use await?, What is IIFE?, How to use Async in IIFE?.
Now you are ready to use async and await in your projects.

Related Keywords:

Async/await Javascript

Async IIFE javascript

Async IIFE useeffect

Async IIFE arrow function

Async function

Async arrow function

Pass async function as parameter javascript

Async variable javascript



Support our IDKBlogs team


Welcome to IDKBlogs.

If our contents help you and you learned from this platform, please consider supporting what we do.

Thank you.




Strongly Recommended Books For You:

Reading books opens the door to allow in more lights, If we want to know a man of rare intellect, try to know what books he/she reads.

For India:

For Other Countries (United States, United Kingdom, Spain, Italy, Germany, France, Canada, etc)


Thank you

I appreciate you taking the time to read this article. The more that you read, the more things you will know. The more that you learn, the more places you'll go. If you’re interested in Node.js or JavaScript this link will help you a lot.

If you found this article is helpful, then please share this article's link to your friends to whom this is required, you can share this to your technical social media groups also. You can follow us on our social media page for more updates and latest article updates.
To read more about the technologies, Please subscribe us, You'll get the monthly newsletter having all the published article of the last month.



Funny Programming | Idkblogs.com | Shubham Verma

👈

Enjoyed this moment?