読者です 読者をやめる 読者になる 読者になる

process.nextTick()その参

setTimeout(function() {
    console.log('====Timeout.====');
}, 0);

var counterFi = 0;
setImmediate(function fi() {
    console.log('immediate. [%d]', ++counterFi);
    if(counterFi < 5) {
        setImmediate(fi);
    }
});

var counterFn = 0;
process.nextTick(function fn() {
    console.log('nextTick [%d]', ++counterFn);
    if(counterFn < 5) {
        process.nextTick(fn);
    }
});

setTimeout()、setImmediate()、process.nextTick()を併用ってのはやってませんでした。どうなるか。

$ node app.js
nextTick [1]
nextTick [2]
nextTick [3]
nextTick [4]
nextTick [5]
====Timeout.====
immediate. [1]
immediate. [2]
immediate. [3]
immediate. [4]
immediate. [5]

I/Oはたぶん、nextTick[5]とTimeoutの間とimmediate[n]各回の後にあると思われ。勉強中なのでまだ確信はないのですが。