以下函數接收一系列鏈接並掃描遠程網站,每頁獲得10篇博文,然後使用async.waterfall
獲得博客文章的每條評論。嵌套的async.eachSeries堆棧迭代
// @param {Array} url
export default function getData(url, cb) {
const arrayOfPosts = [];
// Outer loop
async.eachSeries(url, (link, topLVLcb) => {
// Waterfall
async.waterfall([
// Collects links to posts
callback => {
request(link, (err, response, body) => {
console.log(`working on ${link}`);
const $ = cheerio.load(body);
// OVERALL 10 LINKS PER ONE BLOGPOST
$('.blogpost').each((i, element) => {
// build post ojbect
const post = {
content,
link,
comments: []
}
arrayOfPosts.push(post);
});
callback(null, arrayOfPosts);
});
},
// Looks for details in given post
(arrOfPosts, postDetailsCallback) => {
let counter = 1;
// Inner loop through 10 links
async.eachSeries(arrOfPosts, (post, eachSeriesCallback) => {
request(post.link, (err, response, body) => {
console.log(counter++);
const $ = cheerio.load(body);
$('.comment').each((i, element) => {
// build comment
const comment = {
author,
content
};
post.comments.push(comment);
});
eachSeriesCallback(null);
});
}, postDetailsCallback);
}
], err => {
console.log('DONE PAGE');
console.log('*************************');
topLVLcb(err);
});
}, (result, err) => {
if (err) {
throw err;
} else {
console.log('DONE ALL');
cb(arrayOfPosts);
}
});
}
它提供的輸出是這樣的:
working on www.mywebsite.com/
1
2
3
4
5
6
7
8
9
10
DONE PAGE
**************************************************************
working on www.mywebsite.com/posts/1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
DONE PAGE
**************************************************************
而且這十個遞增每一頁,與每個迭代另起爐竈。它應該掃描每頁10次。我認爲我已經搞砸了一些回調,但我無法弄清楚已經有幾個小時了。這是我第一個nodejs異步代碼,它非常令人難以置信。
那你是一個錯誤第二行.const arrayOfPosts是全局的,每次迭代它增長0,10,20,30,40,50 ....等等! – vkstack