It is widely believed that JavaScript operates on a single-threaded model, but it has the ability to run asynchronously. One intriguing aspect is how this single-threaded model manages non-blocking AJAX requests.
Consider a scenario where a non-blocking AJAX request is initiated in a browser, yet doesn't receive an immediate response. If the event loop continually checks for the response, could execution become blocked? Does the event loop persistently monitor its status and potentially recycle the task by moving it to the back of the macrotask queue if no response is received?
My understanding is that Node.js discreetly spawns threads to handle I/O operations involving disks, databases, and network sockets. But does JavaScript in browsers also generate threads to manage AJAX requests?
A related question arises when examining the following code snippet:
var img = new Image();
img.onerror=function(){alert('error: '+this.src);}
img.onload=function(){alert('image loaded: '+this.src);}
img.src='path/to/image.jpg';
Does the final line in the above code trigger the creation of an additional thread, given its non-blocking nature?