如何在async.eachSeries回调函数中调用回调函数

时间:2017-02-12 09:06:53

标签: javascript node.js asynchronous node-request node-async

我试图在eachSeries完成后调用回调函数,但IT根本不起作用。当它被调用时,它不打印应该打印的2,但是在调用第一个函数后打印4。有什么想法吗?谢谢!

async.waterfall([
      function(callback) {
        console.log("1");
        let eroJson = [];
        rp(optForReddit).then(function(redditJSON) {
          let posts = redditJSON.data.children;
          async.eachSeries(posts, function(item, callback) {
            if (isVideo(item.data.url)) {
              eroJson.push(getAlbumId(item.data.url));
            }
            callback(); // callback function after eachSeries
          }, function() {
            callback(eroJson); // call next callback
          });
        })
      },
      function(redditJSON, callback) {
        console.log("2");
        callback() // call another function
      }
)],
     function(){
         console.log("Last one");
     }

);

1 个答案:

答案 0 :(得分:0)

更改回拨名称。看起来你正在重写回叫名称

    async.waterfall([
          function(waterfallCallback) {
            console.log("1");
            let eroJson = [];
            rp(optForReddit).then(function(redditJSON) {
              let posts = redditJSON.data.children;
              async.eachSeries(posts, function(item, callback) {
                if (isVideo(item.data.url)) {
                  eroJson.push(getAlbumId(item.data.url));
                }
                callback(); // callback function after eachSeries
              }, function() {
                waterfallCallback(eroJson); // call next callback
              });
            })
          },
          function(redditJSON, waterfallCallback) {
            console.log("2");
            waterfallCallback();
          }
    )], function(){
            console.log("Done executing all waterfall functions");
    });