In earlier days of programming every thing was sequential when one operation needed to wait for the others above it to finish. In situation like those of some priority task had to be done then it had to wait for a long to execute the next. So Callbacks and asynchronous programming are the new market introductions.
Callbacks can be in simpler term visualized as a reminder to the Event loop that this has to be executed after a specific interval. As a result after the interval is reached the callbacks get executed on the highest priority basis (But still there’s some limitation which you’ll get to know reading further).
These call backs can be of two types:
– Blocking callbacks (also known as synchronous callbacks or just callbacks)
– Deferred callbacks (also known as asynchronous callbacks)
Lets look at it through an example. We will mainly go through with deferred Callbacks( asynchronous ) with example.
This Code piece demonstrates an asynchronous operation. Where the alert alert(“Finished the call back after 5+ seconds.”); is executed after 5 seconds when the asynchronous operation is completed.
Lets Go through what happens Internally
The SetTimeOut function is added to the Events queue. Providing the Interval 5000 specifies that the function passed with the event has to executed after 5 seconds from the current time, i.e.: the time of addition of the event.