【发布时间】:2015-12-22 11:03:00
【问题描述】:
我需要解析 10 个网页,并捕获它们的主要内容。所以我使用节点可读性并且不想重写相同的函数(只有 url 更改)10 次。最后,我必须计算内容长度。我如何使用循环或任何其他想法来做到这一点? 现在看起来像:
for(var i=0; i<catchedUrl.length; i++){
var data = {length: 0, count: 0};
(function(i) {
read(catchedUrl[i], function(err, article, meta){
if(err) throw err;
var content = article.content;
content = content.split(' ');
article.close();
data.count += 1;
data.length += length;
// Send data to callback when functions done
});
})(i);
}
【问题讨论】:
标签: javascript node.js asynchronous