一般循环中的javascript async/await

leo*_*hel 3 javascript loops async-await ecmascript-2017

我想让这个例子/sf/answers/2351019541/同步.

这是正确的实施吗?

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

        times(5,()=>
               myfunc([1,2,3],err => err)
              )
Run Code Online (Sandbox Code Playgroud)

myfunc 本身就是一个等待各种其他功能的异步功能:

async myfunc(params,cb){

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

}` 
Run Code Online (Sandbox Code Playgroud)

Ber*_*rgi 10

这是正确的实施吗?

是.await如果这是你的实际问题,就像你期望的那样在循环中工作.
不过我会建议写

async function times(n, f) {
    while (n-- > 0)
        await f();
}
Run Code Online (Sandbox Code Playgroud)