0

我想知道 -async只有在前一个步骤在这样的代码中完成后才开始执行下一步?

  async.waterfall([

        function(callback){

           var $ = cheerio.load(result);

               $(".kltat").each(function() {
                  var link = $(this);
                  var text = link.text();

                  text = text.replace(/\./g, "");

                  var statement = searchQuery.replace(/ /g,"_") + text.replace(/ /g,"_");
                  statements.push(statement);

        });

        callback(null,statements);

    },
    function(statements, callback){
    // ....
    callback(null,statements);

  }
 ], function (err, statements) {
    }
4

1 回答 1

0

响应内联:)

 async.waterfall([
  async.constant({url: 'http://abc.xyz'}), 
  scrapePage,
  saveDatabase
], waterfalFinished);


  function scrapPage(request, callback) {
    var $ = cheerio.load(request.url);

    $(".kltat").each(function() { // you could use map instead of each
      var link = $(this);
      var text = link.text();
      text = text.replace(/\./g, "");
      var statement = searchQuery.replace(/ /g,"_") + text.replace(/ /g,"_");
      statements.push(statement);
    });

    // to call the next step in the chain we must call 'callback'
    // maybe if you rename to next makes more sense for you.

    // the first parameter (nodejs convention) is the error. 
    // if you pass error the chain will be halted and jump directly to 
    // waterfallFinished function.

    // the order of arguments you call the CALLBACK matter. it means
    // the order that next step (saveDatabase) is expecting receive the arguments

    callback(null, request, statements);

  }


  function saveDatabase(request, statements, callback) {
    callback(null, requenst , statements);
  }

  function waterfalFinished(err, request, statements) {
    if (err)
     // your error handling here

    console.log(request, statements);
  }

欢呼队友
快乐编码\0/

于 2015-10-23T20:30:42.390 回答