1

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")
}
5
  • 1
    In the second example, you do not pass the function, but its results as a parameter. Hence, there is nothing to wait for in the loop and it will just proceed. Commented Feb 13, 2018 at 14:39
  • I see hm, Can I somehow pass function as parameter ? Commented Feb 13, 2018 at 14:40
  • @trixo: You pass a function by passing the function rather than calling it. So instead of fnBlockLoop(array,timeoutTest(2000)), do fnBlockLoop(array,timeoutTest) Commented Feb 13, 2018 at 14:41
  • 2
    fnBlockLoop(array, async function(){ timeoutTest(2000) } ) or fnBlockLoop(array, timeoutTest, 2000 ). 2nd variant needs some adaptations within the function body of fnBlockLoop() Commented Feb 13, 2018 at 14:42
  • @Sirko Write answer I ll accept :) Commented Feb 13, 2018 at 14:47

2 Answers 2

1

What you want, is to pass the function reference timeoutTest along. What you currently do, is to pass the result of a function call timeoutTest( 2000 ) along. The later is a Promise, which can only resolve once.

To pass the actual function reference along you have to options:

  1. Add an anonymous function wrapper. This will let you use other functions as input: fnBlockLoop(array, async function(){ await timeoutTest(2000) } )
  2. Pass the function reference and its parameters. This needs some adjustments in your fnBlockLoop() function: fnBlockLoop(array, timeoutTest, 2000 )
Sign up to request clarification or add additional context in comments.

4 Comments

I feel like first option does not work tho :) but second does
@trixo Change let test = await func; to let test = await func(); and it should work also. Here you did the same error the other way around: using the reference instead of a call.
hm cant make it run Have no idea where s problem :) Could you try to recreate first option in fiddle ? :)
@trixo My Bad. Forgot a keyword myself. Adding that await makes it work. See this fiddle.
0

You should be passing the function reference like this.

   let array=[10,9,7,6,5,4];
    fnBlockLoop(array,timeoutTest)


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(2000);
        console.log(test)
    }
    console.log("done")
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.