async.each 未在最后调用回调

async.each calls callback not at the end

我正在使用 async.each 函数异步触发一些 I/O 函数,并希望在 all 函数有时使用可选的回调功能继续完成了他们的事情。

然而,当 运行 下面的示例代码时,可以看到如果迭代器是一个同步函数,它工作正常,但它似乎不适用于异步函数。我做错了什么?

此示例代码应在浏览器控制台中运行:

function load (url, callback) {
  var request = new XMLHttpRequest();
  request.open('GET', url, true);
  request.responseType = 'text';
  request.onload = callback;
  request.send();
}

// use async.each with an async function
async.each([1,2,3,4], function iterator(item, callback) {
    load("/", function onload() {
      console.log("inside async func: " + item.toString());
      callback();
    });
  }, function eachFinished(err) {
  console.log("each async func: end");
});

// use async.each with a sync function
async.each([1,2,3,4] , function iterator(item, callback){
  console.log("inside sync func: " + item.toString());
  callback();
  }, function eachFinished(err) {
    console.log("each sync func: end");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/async/1.4.0/async.js"></script>

这给了我输出:

"inside sync func: 1"
"inside sync func: 2"
"inside sync func: 3"
"inside sync func: 4"
"each sync func: end"
"inside async func: 1"
"each async func: end"
"inside async func: 2"
"inside async func: 3"
"inside async func: 4"

我希望在最后调用 "each async func: end" ...

问题是,我使用的是当前主提交而不是 async.js 的发行版本。版本 1.4.0 工作正常。