小编Rxc*_*ard的帖子

Node.js - 在继续代码之前等待多个异步调用完成

所以基本上我有一个带有异步函数的for循环.问题是程序在循环之后才继续,我希望它等到循环中调用的所有异步函数在代码继续之前完成.

在我的代码中,"bar"是一个json数组,其中包含其他json数组.

function write(bla) { // gets called one after another

  for(var url in bla) {
    asyncFunctionCall(url); // Executed about 50 times, it has to run parallel
  }
  // Wait for all called functions to finish before next stuff happens and
  // write gets called again.

}

for(var foo in bar) {
  // Here i parse the json array "foo" which is in the json array "bar"
  write(foo[bla]); // bla is an array of multiple urls.
}
Run Code Online (Sandbox Code Playgroud)

异步函数调用看起来像这样:

var request …
Run Code Online (Sandbox Code Playgroud)

javascript loops asynchronous node.js npm

9
推荐指数
1
解决办法
9133
查看次数

标签 统计

asynchronous ×1

javascript ×1

loops ×1

node.js ×1

npm ×1