在async.waterfall节点js中循环回调

时间:2016-02-24 08:44:40

标签: node.js amazon-web-services asynchronous waterfall

我想在每个循环的else中执行回调。在我的控制台中,我写了“Found”,但回调没有执行......

   async.waterfall([
      function readFile(callback){
        console.log("Start async");
        var params = {Bucket : "MyBucket", Key: "MyKey"};

        reads3.getObject(params, function extract(err,data) {
          //read a json object
          console.log("Start reading");
          callback(err,data);
         });
      },
      function(data, callback){
        var content = data.Body.toString('utf-8').trim();
        var jsonparse = JSON.parse(content);
          async.each(config, function(item) {
          var currentPath = item.path;
            if((key.search(currentPath)) === (-1)) {
              console.log("No found !");
            } else {
                console.log("Found");
                callback(jsonparse);
            }
          });
      },
      function(jsonparse){      
        console.log("In the 2nd loop !");
      }
    ]);

1 个答案:

答案 0 :(得分:0)

你可以试试吗

 async.waterfall([
          function readFile(callback){
            console.log("Start async");
            var params = {Bucket : "MyBucket", Key: "MyKey"};

            reads3.getObject(params, function extract(err,data) {
              //read a json object
              console.log("Start reading");
              callback(err,data);
             });
          },
          function(data, callback){
            var content = data.Body.toString('utf-8').trim();
            var jsonparse = JSON.parse(content);
              async.each(config, function(item) {
              var currentPath = item.path;
                if((key.search(currentPath)) === (-1)) {
                  console.log("No found !");
                } else {
                    console.log("Found");
                    callback(null,jsonparse);
                }
              });
          },
          function(jsonparse,callback){      
            console.log("In the 2nd loop !");
            callback(null,'result');
          }
        ],function(err,data){
//your data is result
//......Your function script
});