nodejsasynchronous嵌套调用

我想要废除一个url:

1请求获取元素列表

1请求每个结果得到细节

这里我有什么:

var request = require('request') , cheerio = require('cheerio') , async = require('async') , format = require('util').format; var baseurl = 'http://magiccards.info'; async.waterfall([ function (callback) { request(baseurl + '/sitemap.html', function (err, response, body) { var sets = []; var $ = cheerio.load(body); $('a[href$="/en.html"]').each(function () { sets.push({"name": $(this).text(), "code":$(this).attr('href').match(/\/([^)]+)\//)[1], "path": $(this).attr('href'), "translations":[]}); }); callback(null, sets); }); }, function (sets, callback) { console.log(sets); async.eachSeries(sets, function (set, callback) { console.log('SET ' + set.code.toUpperCase()); request(baseurl + set.path, function (err, response, body) { var $ = cheerio.load(body); $('body > a[href^="/' + set.code + '/"]').each(function () { console.log(' %s (%s)', $(this).text(), $(this).attr('href')); }); }); }); } ], function (err, result) { console.log('ERR'); // result now equals 'done' }); 

问题是第二个瀑布函数只运行一次,如果我用eachreplaceeachSeries,循环运行X次(但我需要等待结果)。

我错过了什么?

您需要调用eachSeries callback函数。 否则async不会知道你已经完成了。 (1)

您还需要通过调用callback函数来告诉waterfall函数您已经完成了该步骤。 (2)

 function (sets, waterfallCallback) { async.eachSeries(sets, function (set, seriesCallback) { console.log('SET ' + set.code.toUpperCase()); request(baseurl + set.path, function (err, response, body) { var $ = cheerio.load(body); $('body > a[href^="/' + set.code + '/"]').each(function () { console.log(' %s (%s)', $(this).text(), $(this).attr('href')); }); seriesCallback(null); /* 1 */ }); }, waterfallCallback /* 2 */); }