我正在尝试遵循[ Node 学校][1]的练习。有一项练习需要收集三个流,并且仅在所有三个流完成后才打印输出。不使用任何第三方模块。
有人可以指出为什么我的方法不起作用吗?它陷入了无限循环:
var http = require('http');
var concat = require('concat-stream');
var count = 3;
var str1, str2, str3;
http.get(process.argv[2], function (response) {
response.pipe(concat(function(data) {
str1 = data.toString();
--count;
}));
});
http.get(process.argv[3], function (response) {
response.pipe(concat(function(data) {
str2 = data.toString();
--count;
}));
});
http.get(process.argv[4], function (response) {
response.pipe(concat(function(data) {
str3 = data.toString();
--count;
}));
});
function foo() {
if (count > 0) {
process.nextTick(foo);
} else {
console.log(str1);
console.log(str2);
console.log(str3);
}
};
foo();
最佳答案
http.get()
回调在事件循环的下一个刻度或更晚之前无法运行。 process.nextTick()
在事件循环的前面放置一些东西,在已经存在的回调之前。
您的递归例程永远不会停止递归,因为它正在等待这些回调来递减计数器,但它们永远不会触发。
如果将 process.nextTick()
替换为 setImmediate()
,它可能会起作用。 (我没有测试过,如果你这样做了,嘿,请告诉我它是否有效。)
但我想说的是完全摆脱递归。不需要。你可以(例如)做这样的事情:
var count = 0;
var httpGet = function (index) {
http.get(process.argv[2 + index], function (response) {
// Do stuff here
// This next bit will probably end up inside the callback provided to concat
count++;
if (count === 3) {
// Print results here
}
})
};
for (var i = 0; i < 3; i++) {
httpGet(i);
}
关于node.js - Nodejs事件循环,如何正确使用nextTick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30515037/