异步
for (var index = 0; index < data.length; index++) {
var req = http.request(urlEntity, function(res) {
var content = "";
res.on("data", function(data) {
content += data;
});
res.on('end', function() {
// 获取到数据
......
});
});
req.on("error", function(e) {
if(e){
console.info(e);
// 遇到异常
}
});
}
//往下执行
同步
var data = [];
(function getdata(index) {
if(index>=data.length) return true;
var queryUrl = data[index];
var req = http.request(urlEntity, function(res) {
var content = "";
res.on("data", function(data) {
content += data;
});
res.on('end', function() {
//do something u want!
getdata(index+1);
});
});
req.on("error", function(e) {
if(e){
console.info(e);
}
});
})(0)