Last active
January 26, 2021 19:54
-
-
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
This file contains 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
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"); |
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
1 first
2 message:hello!
3 last
4 nextTick
5 stat
6 immediate
7 quick timer
8 long timer