我在节点是新的,所以下面的代码的行为是不明确的对我说:的Node.js:()setImmediate后http.request()触发
function step(iteration) {
if (iteration === 10) return;
process.nextTick(() => {
step(iteration + 1); // Recursive call from nextTick handler.
console.log(`nextTick iteration: ${iteration}`);
});
http.request({hostname: '127.0.0.1', port: 5000}, (response) => {
console.log("here !");
}).end();
setImmediate(() => {
console.log(`setImmediate iteration: ${iteration}`);
});
}
step(0);
OUTPUT:
nextTick iteration: 0
...
nextTick iteration: 9
setImmediate iteration: 0
...
setImmediate iteration: 9
here !
here !
here !
here !
here !
here !
here !
here !
here !
here !
问题是:
1)为什么http.request()
回调是在setImmediate()
之后触发的?我不明白,为什么setImmediate()
通常被称为:它的回调是在循环中注册的。但正如文件所述,setImmediate()
是检查阶段,应该在事件循环后处理轮询一。如果我理解正确,http.request()
后nextTick()
应该是轮询阶段;
2)但是如果http.request()
没有在轮询中处理,它发生在什么阶段?特别是考虑到它在setImmediate()
之后有效。
我的Node.js版本是6.11.2。预先感谢您的解释。
它稍后记录它,因为它正在等待*响应* – 2017-08-24 22:31:51