Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通用循环中的javascript异步/等待_Javascript_Loops_Async Await_Ecmascript 2017 - Fatal编程技术网

通用循环中的javascript异步/等待

通用循环中的javascript异步/等待,javascript,loops,async-await,ecmascript-2017,Javascript,Loops,Async Await,Ecmascript 2017,我想让这个例子同步 这是正确的实现吗 let times= async (n,f)=>{while(n-->0) await f();} times(5,()=> myfunc([1,2,3],err => err) ) myfunc本身就是一个异步函数,等待各种其他函数: async myfunc(params,cb){ await a( err => err

我想让这个例子同步

这是正确的实现吗

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

        times(5,()=>
               myfunc([1,2,3],err => err)
              )
myfunc
本身就是一个异步函数,等待各种其他函数:

async myfunc(params,cb){

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

}` 
这是正确的实现吗

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

        times(5,()=>
               myfunc([1,2,3],err => err)
              )
对<代码>等待在循环中工作,就像您期望的那样,如果这是您的实际问题。
不过,我建议你写信

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

async/await
不会使代码同步。它只是提供了一种编写异步代码的更方便的方法。是的,这看起来应该是可行的(为什么不试试呢?),尽管我宁愿写
async()=>wait-myfunc([1,2,3],err=>err)
。你说的“同步”是什么意思?或者这是一个输入错误,您的意思是异步的?您正在传递但未在任何地方使用的
err=>err
回调看起来不像是正确使用
async函数。