I have several async functions and I want all of them to run synchronously. So tried doing it using promises but it isn't waiting for the third function to complete. It returns to loop after asyncDataRequestOne request.
I searched but I am not able to find examples for chaining promises as below or similar.
Here is what I have tried
Trial 1
function three(j){
return new Promise((resolve,reject)=>{
asyncDataRequestThree(()=>{
let k = j + 14;
resolve();
});
});
}
function two(i){
return new Promise((resolve, reject)=>{
asyncDataRequestOne().then(()=>{
asyncDataRequestTwo().then(()=>{
let = i+ 7;
three(j);
resolve()
});
});
});
}
function one(){
//Each loop should run only after function three's resolve
arr.forEach((i) => {
two(i);
}
}
Trial 2
function async three(j){
await asyncDataRequestThree(()=>{
let k = j + 14;
});
});
}
function async two(i){
await asyncDataRequestOne().then( async ()=>{
await asyncDataRequestTwo().then( async ()=>{
let = i+ 7;
await three(j);
});
});
}
function one(){
//Each loop should run only after function three's resolve
arr.forEach( async () =>{
await two(i);
}
}
Thank you