Async.map和async.waterfall没有按预期方式一起工作

时间:2015-07-20 12:35:15

标签: javascript node.js async.js

我正在使用Asyncasync.map()使用函数绑定我的数据数组,并在该函数中async.waterfall()运行一系列函数,但瀑布不能按预期工作。我也试过系列但没有成功。

var myData = [1,2,3];
async.map(myData, myExport, function (err, result) {
  if(!err) {
    console.log('Finished: ' + result);
    } else {
    console.log('Error: ' + err);
}});

function myExport(item, callback) {
  console.log('item Value: ', item );
  async.waterfall([function(cb){
    console.log(' -> exportData: ', item);

  },function(response,cb){
    console.log(' -> saveData: ', item);
  },
  function(response,cb){
    console.log(' -> updateData: ', item);
  }], function(err,result){
    if(!err) {
      console.log('Perfect: ',result);
    } else {
      console.log('Error: ',err);
    }
  })
}

输出:

item Value:  1
 -> exportData:  1
item Value:  2
 -> exportData:  2
item Value:  3
 -> exportData:  3

必填项:

item Value:  1
 -> exportData:  1
 -> saveData:    1
 -> updateData:  1
item Value:  2
 -> exportData:  2
 -> saveData:    2
 -> updateData:  2
item Value:  3
 -> exportData:  3
 -> saveData:    3
 -> updateData:  3

2 个答案:

答案 0 :(得分:4)

使用async时,请务必致电回调!在您的瀑布方法中,请务必调用cb(null, item),以便瀑布知道您何时继续。在映射函数myExport中,请务必在瀑布完成后调用callback

工作代码:

    var async = require('async');

    var myData = [1,2,3];
    async.mapSeries(myData, myExport, function (err, result) {
      if(!err) {
        console.log('Finished: ' + result);
        } else {
        console.log('Error: ' + err);
    }});

    function myExport(item, callback) {
      console.log('item Value: ', item );
      async.waterfall([function(cb){
        console.log(' -> exportData: ', item);
          cb(null, item);

      },function(response,cb){
        console.log(' -> saveData: ', item);

          cb(null, item);
      },
      function(response,cb){
        console.log(' -> updateData: ', item);

          cb(null, item);
      }], function(err,result){
        if(!err) {
          console.log('Perfect: ',result);
        } else {
          console.log('Error: ',err);
        }
          callback( err, result);
      })
    }

输出:

    chimmelb:~/Documents/workspace/scratch$ node index.js 
    item Value:  1
     -> exportData:  1
     -> saveData:  1
     -> updateData:  1
    Perfect:  1
    item Value:  2
     -> exportData:  2
     -> saveData:  2
     -> updateData:  2
    Perfect:  2
    item Value:  3
     -> exportData:  3
     -> saveData:  3
     -> updateData:  3
    Perfect:  3
    Finished: 1,2,3

答案 1 :(得分:2)

您必须调用传递给waterfall函数的回调,以便在您完成后通知它,例如:

function myExport(item, callback) {
  console.log('item Value: ', item );
  async.waterfall([function(cb){
    console.log(' -> exportData: ', item);
    cb(null, item);
  }, function(response,cb){
    console.log(' -> saveData: ', response);
    cb(null, response);
  }, function(response,cb){
    console.log(' -> updateData: ', response);
    cb(null, response);
  }], function(err,result){
    if(!err) {
      console.log('Perfect: ',result);
    } else {
      console.log('Error: ',err);
    }
  })
}