I would like to ask why this async loop works, but when i change it to use function as parameter it just prints out 1 testing and then just jumps at the end like nothing happens. Any Idea ? Is it cause function can be used just once or ?. Or do you have a better way how to do a async loop in sequence ?
let array=[10,9,7,6,5,4];
fnBlockLoop(array)
function timeoutTest(delay){
return new Promise((resolve)=>{
console.log("testing")
setTimeout(resolve,delay)
})
}
async function fnBlockLoop(input,func){
for(const item of input){
let test = await timeoutTest(2000);
console.log(test)
}
console.log("done")
}
2
let array=[10,9,7,6,5,4];
fnBlockLoop(array,timeoutTest(2000))
function timeoutTest(delay){
return new Promise((resolve)=>{
console.log("testing")
setTimeout(resolve,delay)
})
}
async function fnBlockLoop(input,func){
for(const item of input){
let test = await func;
console.log(test)
}
console.log("done")
}
fnBlockLoop(array,timeoutTest(2000)), dofnBlockLoop(array,timeoutTest)fnBlockLoop(array, async function(){ timeoutTest(2000) } )orfnBlockLoop(array, timeoutTest, 2000 ). 2nd variant needs some adaptations within the function body offnBlockLoop()