-
-
Save boolfalse/5f2c7160a6478da492634facc0c416e9 to your computer and use it in GitHub Desktop.
NGNT JS - event-loop snippets
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
// example 01: microtask and timer queues | |
setTimeout(() => console.log("setTimeout 1"), 0); | |
setTimeout(() => { | |
console.log("setTimeout 2"); | |
process.nextTick(() => console.log("setTimeout 2 nextTick")) | |
}, 0); | |
setTimeout(() => console.log("setTimeout 3"), 0); | |
process.nextTick(() => console.log("nextTick 1")); | |
process.nextTick(() => { | |
console.log("nextTick 2"); | |
process.nextTick(() => console.log("nextTick 2 nextTick")); | |
}); | |
process.nextTick(() => console.log("nextTick 3")); | |
Promise.resolve().then(() => console.log("Promise 1")); | |
Promise.resolve().then(() => { | |
console.log("Promise 2"); | |
process.nextTick(() => console.log("Promise 2 nextTick")); | |
}); | |
Promise.resolve().then(() => console.log("Promise 3")); | |
/* | |
Result: | |
nextTick 1 | |
nextTick 2 | |
nextTick 3 | |
nextTick 2 nextTick | |
Promise 1 | |
Promise 2 | |
Promise 3 | |
Promise 2 nextTick | |
setTimeout 1 | |
setTimeout 2 | |
setTimeout 3 | |
setTimeout 2 nextTick | |
*/ |
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
// example 02: I/O queue and timer queue anomaly | |
const fs = require("fs"); | |
setTimeout(() => console.log("setTimeout"), 0); | |
fs.readFile(__filename, () => console.log("readFile")); | |
// for (let i = 0; i < 1000000000; i++) {} | |
/* | |
Result: | |
setTimeout | |
readFile | |
// anomaly | |
readFile | |
setTimeout | |
*/ |
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
// example 03: I/O queue and I/O polling | |
const fs = require("fs"); | |
fs.readFile(__filename, () => { | |
console.log("readFile"); | |
}); | |
process.nextTick(() => console.log("nextTick")); | |
Promise.resolve().then(() => console.log("Promise")); | |
setTimeout(() => console.log("setTimeout"), 0); | |
setImmediate(() => console.log("setImmediate")); | |
for (let i = 0; i < 1000000000; i++) {} | |
/* | |
Result: | |
nextTick | |
Promise | |
setTimeout | |
setImmediate | |
readFile | |
*/ |
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
// example 04: check queue & timer queue anomaly | |
setTimeout(() => console.log("setTimeout")); | |
setImmediate(() => console.log("setImmediate")); | |
/* | |
setTimeout | |
setImmediate | |
// anomaly | |
setImmediate | |
setTimeout | |
*/ |
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
// example 05: Close queue in the end | |
const fs = require("fs"); | |
const readableStream = fs.createReadStream(__filename); | |
readableStream.on("close", () => { | |
console.log("readableStream"); | |
}); | |
readableStream.close(); | |
setImmediate(() => console.log("setImmediate")); | |
setTimeout(() => console.log("setTimeout"), 0); | |
Promise.resolve().then(() => console.log("Promise")); | |
process.nextTick(() => console.log("nextTick")); | |
/* | |
Result: | |
nextTick | |
Promise | |
setTimeout | |
setImmediate | |
readableStream | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment