Last active
April 17, 2017 20:32
-
-
Save lukealbao/e9c596c79aa27eac215f6ebd6028f4fd to your computer and use it in GitHub Desktop.
process.nextTIck
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// https://nodejs.org/api/process.html#process_process_nexttick_callback_args | |
// | |
// Says: | |
// | |
// "It runs before any additional I/O events (including timers) fire | |
// in subsequent ticks of the event loop." | |
// | |
// Question is, does "additional" mean events whose listeners are added | |
// _additionally_, i.e., after the =nextTick= call, or additional as in anything | |
// that isn't the cb passed to =nextTick=? (Answer: the latter. It will block any async | |
// events, no matter when they are registered.) | |
// | |
// Uncomment function calls below for demo: | |
const fs = require('fs'); | |
const stream = fs.createReadStream('/dev/random', { | |
encoding: 'base64', flags: 'r' | |
}); | |
setTimeout(process.exit, 3e3); // =starve= will even starve this, though. | |
stream.on('data', chunk => console.log(`${chunk.length} bytes from the wire`)); | |
function starve () { | |
console.log('So hungry...'); | |
process.nextTick(starve); | |
} | |
function interleave () { | |
console.log('Excuse me, sir...?'); | |
setTimeout(interleave, 0); | |
} | |
// Uncomment this to block I/O: | |
// starve(); | |
// Uncomment this to not block: | |
// interleave(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment