Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the Event Loop from Node.js code?

Tags:

node.js

Is there a native function that can be executed from Node.js code which outputs an array or an object containing the information about what's currently supposed to be in the Event Loop?

like image 968
laggingreflex Avatar asked Jun 10 '15 13:06

laggingreflex


1 Answers

You're looking for a uv_loop.

There's a UV_EXTERN void uv_walk(uv_loop_t* loop, uv_walk_cb walk_cb, void* arg); method you can call to iterate the event loop.

Unfortunately, it's not really exposed to JavaScript - so unless you want to do this for fun (and fork node/io.js) then no.

You can wrap calls to the event loop and monitor those but that's slow and not even close - the actual loop isn't in JS land, only small parts of it - and even parts of that (like the microtask queue) are in C++.

A lot of people think of the event loop like an array - it's actually more like:

enter image description here

It's a lot easier to hook on process.nextTick and the timers than on the poll part mentioned above.

like image 182
Benjamin Gruenbaum Avatar answered Oct 12 '22 18:10

Benjamin Gruenbaum