How to run functions iteratively with async await in JavaScript?

JavascriptWeb DevelopmentObject Oriented Programming

You can use keyword async as well as await. Following is the code −

Example

async function test(i) {
   while (i <= 10) {
      await demo(" Call Demo1()");
      await demo(" Call Demo2()");
      i++;
   }
}
async function demo(value){
   console.log(value);
}
test(1);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo73.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo73.js
Call Demo1()
Call Demo2()
Call Demo1()
Call Demo2()
Call Demo1()
Call Demo2()
Call Demo1()
Call Demo2()
Call Demo1()
Call Demo2()
Call Demo1()
Call Demo2()
Call Demo1()
Call Demo2()
Call Demo1()
Call Demo2()
Call Demo1()
Call Demo2()
Call Demo1()
Call Demo2()
raja
Published on 07-Sep-2020 07:26:39
Advertisements