The Event Loop
The event loop is what allows Node.js to perform non-blocking I/O operations despite being single-threaded.
console.log("1: Start");\n\nsetTimeout(() => console.log("2: Timeout"), 0);\n\nPromise.resolve().then(() => console.log("3: Promise"));\n\nprocess.nextTick(() => console.log("4: nextTick"));\n\nconsole.log("5: End");\n\n// Output: 1, 5, 4, 3, 2Event Loop Phases
- timers: executes setTimeout/setInterval callbacks
- pending callbacks: I/O callbacks deferred to next iteration
- idle, prepare: internal use
- poll: retrieve new I/O events
- check: setImmediate callbacks
- close callbacks: close event callbacks
Global Objects
// In Node.js, these are global:\nglobalThis; // The global object (like window in browsers)\nconsole; // Console for logging\nprocess; // Current Node.js process\nBuffer; // Handle binary data\n__dirname; // Current directory path\n__filename; // Current file path\nsetTimeout; // Schedule a one-time callback\nsetInterval; // Schedule a repeating callback\nsetImmediate;// Schedule callback for check phase