我使用async的eachSeries方法来抓取一系列RSS提要,然后构建一个对象以传递给async的瀑布方法(代码中没有特色)。
为了允许回调调用next()函数,并允许构建RSS源数组,我使它们可以在窗口对象上全局访问。
我无法帮助,但我觉得有一种更好的方法来设计它,而不需要完全金字塔,也可以使用最佳实践来避免可变碰撞。
以下是代码:
var async = require('async');
var parser = require('rssparser');
// let me know if you would like me to post the rssFeeds array object
var numberOfItemsToScrape = 1
// receives the scraped data and builds the object
function loadData(err, data){
var items = [];
for (var i = 0; i <= numberOfItemsToScrape - 1; i++) {
items.push(data.items[i])
}
feedToMutate.items = items
rssFeedsToExport.push(feedToMutate)
nextFunction();
}
// scrapes the data and passes it to a callback
function scrapeRss(rssFeed){
parser.parseURL(rssFeed.url, parserOptions, loadData)
}
// calls async.eachSeries on the rssFeeds array
function scrapeRssAndLoadData(rssFeeds, cb){
rssFeedsToExport = []
async.eachSeries(rssFeeds, function(feed, next) {
scrapeRss(feed);
feedToMutate = feed
nextFunction = next
},
function (err) {
// when used in the context of the async.waterfall pattern,
// this will look like cb(null, rssFeedsToExport)
console.log(rssFeedsToExport);
});
}
scrapeRssAndLoadData(rssFeeds, cb)
有没有更好的方法来设计此代码以避免变量冲突?