Skip to content

Instantly share code, notes, and snippets.

@a0viedo
Last active January 26, 2021 19:54
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a0viedo/0de050bb2249757c5def to your computer and use it in GitHub Desktop.
Save a0viedo/0de050bb2249757c5def to your computer and use it in GitHub Desktop.
Can you guess what the console will print? Extracted from the book Mastering Node.js
var fs = require('fs');
var EventEmitter = require('events').EventEmitter;
var pos = 0;
var messenger = new EventEmitter();
messenger.on('message', function(msg) { console.log(++pos + " message:" + msg);
});
console.log(++pos + " first");
process.nextTick(function(){
console.log(++pos + " nextTick");
});
messenger.emit('message', "hello!");
fs.stat(__filename, function(){
console.log(++pos + " stat");
});
setTimeout(function(){
console.log(++pos + " quick timer");
}, 0);
setTimeout(function(){
console.log(++pos + " long timer");
}, 30);
setImmediate(function(){
console.log(++pos + " immediate");
});
console.log(++pos + " last");
@tomasvts
Copy link

1 first
2 message:hello!
3 last
4 nextTick
5 stat
6 immediate
7 quick timer
8 long timer

@nurrony
Copy link

nurrony commented Oct 29, 2015

When I use fs.readfile on NodeJS v4.2.1 output is like below

1 first
2 message:hello!
3 last
4 nextTick
5 quick timer
6 immediate
7 readFile
8 long timer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment