I am wondering how JavaScript callbacks work. I specifically can’t understand how asynchronous XmlHttpRequest works. How can JS determine that server returned some value (in one thread), and when to call callback method? Is it build on timers?
I am wondering how JavaScript callbacks work. I specifically can’t understand how asynchronous XmlHttpRequest
Share
A very similar question was answered here in more detail.
The basic answer is that the underlying networking is happening at the OS level where there can be threads or some type of notifications when incoming networking packets arrive. When the result has completed, an event is added to the javascript event queue. When that event gets to the top of the event queue and javascript is ready to act on it, the proper javascript ajax event will be triggered which starts the chain of javascript that results in calling your callback.
There may some timers involved for timeouts, but timers are not used to know when the ajax response has arrived. That’s based on the OS level networking support that the browser uses.