Event loop vs Callback function in node.js

1.8k views Asked by At

What is the difference between Event Loop and Callback function in node.js?

1

There are 1 answers

1
ema On

Event Loop is in the underlying system that execute your program. Every time your code needs to access an external resource (for example a XHR call to an api) you usually do something like that:

callToTheApi('/api/some/url', function(response) { ... the callback code ...}

The runtime execute the call and pass the callback function (second parameter of the callToTheApi function) to the Event Queue. This means 'Execute the call to the api and when the api has done please tell me and go on with the call back'. The event loop while the api are doing their job put in execution some other piece of code and resume the callback once the api has done.

You can find a good explanation here: Understanding the Node.js Event Loop