I'm new in Node, so the following code behavior is not clear to me:
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 !
Questions are:
1) why http.request()
callback is triggered after setImmediate()
? I undestand, why setImmediate()
is generally called: its callbacks were registered during the loop. But as documentation says, setImmediate()
is a check phase, which should be processed in event loop after poll one. If I understood correctly, http.request()
after nextTick()
should be that poll phase;
2) but if http.request()
is not handled in poll, in what phase does it happen? Especially considering that it works after setImmediate()
.
My Node.js version is 6.11.2. Thanks in advance for explanation.