我想知道 - 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) {
}
答案 0 :(得分: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 /