Nodejs async.series没有执行所有方法

时间:2016-09-08 19:58:16

标签: node.js

我正在尝试使用'async'来完成我的工作,因此我编写了一个示例程序以确保其有效。 async.parallel()按预期工作,但不是async.series()。不确定我错过了什么。任何人都可以看看这个示例代码并指出问题/错误吗?

async.series([task1,task2])只是执行'task1'。

const async = require('async');
var firstThing = function() {
  setTimeout(function(){console.log('IN the First thing')}, 1000);
};

var secondThing = function () {
  setTimeout(function(){console.log('IN the second thing')}, 1500); 
};

async.series(
 [
  firstThing, 
  secondThing
 ],
 function (err, result) {
    console.log('blah blah '+result);
});

当我运行此代码时,我得到了

IN the First thing

并退出。为什么没有调用第二个任务?我错过了什么?

感谢。

1 个答案:

答案 0 :(得分:2)

当你完成要串联运行的每个功能时,你必须回电话:

const async = require('async');
var firstThing = function(callback) {
  setTimeout(function(){console.log('IN the First thing')}, 1000);
  callback(/* pass error or callback*/);
};

var secondThing = function (callback) {
  setTimeout(function(){console.log('IN the second thing')}, 1500); 
  callback(/* pass error or callback*/);
};