Menu

You Know the JavaScript Event Loop. But Node.js Has Another One
πŸ“°

You Know the JavaScript Event Loop. But Node.js Has Another One

DEV Community: nodeΒ·KristinZΒ·about 17 hours ago
#MmUrUmcW
#devtonodejs#devto#dev#enter#exit
Long article (3744 words) β€” no AI summary yet.
Reading 0:00
15s threshold

If you've worked with JavaScript for a while, you probably already understand the event loop.

You know that this:

console . log ( 'A' ) ;

Promise . resolve ( ) . then ( ( ) => { console . log ( 'B' ) ; } ) ;

setTimeout ( ( ) => { console . log ( 'C' ) ; } , 0 ) ;

console . log ( 'D' ) ;

Enter fullscreen mode

Exit fullscreen mode

prints:

A D B C

Enter fullscreen mode

Exit fullscreen mode

You probably have a mental model something like this:

Synchronous code ↓ Microtasks ↓ Tasks ↓ Microtasks ↓ Tasks ↓ ...

Enter fullscreen mode

Exit fullscreen mode

That's a useful model for browser JavaScript.

Then you start writing a Node.js server.

And suddenly someone shows you this:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ timers β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ ↓ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ pending β”‚ β”‚ callbacks β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ ↓ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ poll β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ ↓ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ check β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ ↓ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ close β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β”‚ └──────► timers

Enter fullscreen mode

Exit fullscreen mode

And you're told:

"This is the…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.