Skip to content

A minimal simulation of Node.js event loop phases, including nextTick, microtasks, timers, poll, check, and close callbacks.

License

Notifications You must be signed in to change notification settings

ylcnfrht/nodejs-event-loop-simulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸŒ€ nodejs-event-loop-simulator

A minimal and educational simulation of Node.js event loop phases β€” including process.nextTick, microtasks (Promise.then), timers, poll, check, and close callbacks.


πŸ“š Overview

This project is designed to help developers understand the internal workings of the Node.js Event Loop by simulating each phase step-by-step.

It supports queuing callbacks for:

  • nextTick (like process.nextTick)
  • Microtasks (like Promise.then)
  • Timers (like setTimeout)
  • Pending callbacks (e.g., I/O like fs.readFile)
  • Idle/Prepare (internal phases)
  • Poll (I/O polling)
  • Check (like setImmediate)
  • Close callbacks (e.g., socket .on('close'))

πŸš€ Getting Started

1. Clone the repository

git clone https://github.com/your-username/event-loop-simulator.git
cd event-loop-simulator

2. Run the simulation

node index.js

✨ Example Usage

const EventLoopSimulator = require('./event-loop');

const eventLoop = new EventLoopSimulator();

eventLoop.queueNextTick(() => console.log('nextTick callback'));
eventLoop.queueMicrotask(() => console.log('microtask callback'));
eventLoop.queueTimer(() => console.log('timer callback'));

eventLoop.tick();

πŸ”„ Supported Phases and Methods

Phase ------------------------ Method
Next Tick -------------------- queueNextTick(cb)
Microtask -------------------- queueMicrotask(cb)
Timer ------------------------ queueTimer(cb)
Pending Callback ------------- queuePendingCallback(cb)
Idle/Prepare ----------------- queueIdlePrepare(cb)
Poll ------------------------- queuePoll(cb)
Check ------------------------ queueCheck(cb)
Close ------------------------ queueCloseCallback(cb)

🎯 Purpose

This simulator is intended for educational purposes only. It helps visualize the order and behavior of various phases in the Node.js event loop without using real asynchronous APIs.

πŸ“„ License

MIT License

About

A minimal simulation of Node.js event loop phases, including nextTick, microtasks, timers, poll, check, and close callbacks.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published