I'm developing an app and I'm having a problem. I want to run two functions inside a forEach loop, which means for each iteration run both functions once.
This is very simple to do when you think about it, but my functions are asynchronous and my first function needs to finish before the second one executes.
Reason why i made these functions async is that in my app these functions represent an API call.
I will show you a quick example in which i used setTimeout() instead of API calls.
async function f(){
await setTimeout(()=>{
console.log("FIRST")
},1000)
}
async function f2(){
await setTimeout(()=>{
console.log("SECOND")
},3000)
}
for(var i = 0;i < 5; i++){
f()
f2()
}
When I run this the output is:
FIRST
FIRST
FIRST
FIRST
FIRST
SECOND
SECOND
SECOND
SECOND
SECOND
And i need my output to be:
FIRST
SECOND
FIRST
SECOND
FIRST
SECOND
FIRST
SECOND
FIRST
SECOND
Any idea how to achieve this? Would be very helpful.