Skip to content

Instantly share code, notes, and snippets.

@a0viedo a0viedo/asyncQuiz.js
Last active Oct 17, 2019

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Jun 22, 2015

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

@nmrony

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.