Async Await in Do While Loop

Async Await in Do While Loop

In this post, we are going to checkout Async Await in Do while loop with examples. Do while Loop in Typescript is same as the Do While Loop in Javascript.

How to use async and await in the do-while loop?

Async and await are used for asynchronous operations.

Async Await in Do While Loop example:


const getData = async _ => {
    let isFlag = false;
    do{
         let res = await fetch('/api/get') 
         if (res.status===200) isFlag = true;
       }
     while(!isFlag) 
}   

Calling getData method which is an async function, pause the execution of await until promise returns a response, and do while loop works as expected.


Happy Coding!

πŸ”₯ 3 Views
Sep 2, 2022