3

I want to make this example https://stackoverflow.com/a/33585993/1973680 synchronous.

Is this the correct implementation?

        let times= async (n,f)=>{while(n-->0) await f();} 

        times(5,()=>
               myfunc([1,2,3],err => err)
              )

myfunc is itself an async function awaiting for various other functions:

async myfunc(params,cb){

   await a( err => err )
   await b( err => err )
   await c( err => err )

}` 
3
  • 2
    async/await doesn't make code synchronous. It just enables a more convenient way of writing asynchronous code. And yeah, that looks like it should work (why don't you just try it?), although I'd rather write async () => await myfunc([1,2,3],err => err). Commented Dec 14, 2016 at 0:19
  • 1
    What do you mean by "make synchronous"? Or is it a typo and you meant asynchronous? Commented Dec 14, 2016 at 1:16
  • 1
    That err => err callback you are passing but not using anywhere does not look like correct usage of async functions. Commented Dec 14, 2016 at 1:17

1 Answer 1

10

Is this the correct implementation?

Yes. await works in loops like you'd expect it to, if that was your actual question.
I would however recommend to write

async function times(n, f) {
    while (n-- > 0)
        await f();
}
Sign up to request clarification or add additional context in comments.

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.