Having read through dozens of articles and documents describing the Node.js event loop, such as the one provided by Node.js themselves: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
I simply cannot wrap my head around this: WHY does the event loop require several phases, each with their own callback queues?
All the documents and articles describe the phases of the loop in terms of "this phase does so and so and executes callbacks set with X or Y", but never really expound on WHY these separate queues are necessary in the first place.
Why would the callbacks of a setTimeout()
or setImmediate()
or socket closings need to be executed at a different point than the polling phase where supposedly the vast majority of callbacks are executed?
if the callback queue in the polling phase is exhausted before moving to the next phase anyway, why not just have one queue that is interrupted for whatever non-queue related actions are performed in the other phases?