horaceshmorace's notebooks

  • Node.js Event Loop - /horaceshmorace/node-js-event-loop
    Last edited a month ago
    /** * Demonstrates the order of execution of various actions in the Node.js event loop. * * Actions are numbered in the order they appear in the code to help contrast with the actual order they will be console.logged. */ const fs = require('fs') // 1: Opening inline statement console.log('1. BEGIN INLINE EXECUTION') fs.createReadStream('./index.js') // 2: FS open event .on('open', () => console.log('2. FILE SYSTEM')) // 3: FS close event .on('close', () => console.log('3. CLOSE EVENT')) .close() // 4: setImmediate setImmediate(() => console.log('4. SET IMMEDIATE')) // 5: setInterval with 0 ttl setInterval(function () { console.log('4.5. SET INTERVAL') clearInterval(this) }, 0) // 5: setTimeout setTimeout(() => console.log('5. SET TIMEOUT'), 0) // 6: setInterval with 0 ttl setInterval(function () { console.log('6. SET INTERVAL') clearInterval(this) }, 0) // 7: nextTick process.nextTick(() => console.log('7. NEXT TICK')) // 8: Closing inline statement console.log('8. END INLINE EXECUTION') '...Entering the Event Loop!...'