问题
If Javascript is a single threaded process and AJAX is asynchronous then how does it happen ? So at OS level ain't the JS engine making a Non-Blocking I/O call for Ajax ?
回答1:
Yes, the browser engine is making a non-blocking I/O call for Ajax (when you do a non-blocking ajax call).
There are any number of different ways the browser could implement the ajax networking. The only thing we know for sure is that the ajax i/o request isn't blocking the javascript thread. And, further every browser is free to implement it differently as long as they don't block the JS execution thread and any other threads needed to keep the browser functional during the ajax call.
Under the covers, inside the browser, it could be using a separate OS thread to run the ajax call in a blocking fashion on that thread, it could be using non-blocking i/o on a separate thread, it could be using non-blocking i/o on the javascript interpreter thread (probably unlikely, but possible). It could even be using a separate process to manage networking operations with IPC to communicate between them. Which it chooses is entirely up to the browser implementation as any of those methods will allow the javascript interpreter to keep running while the ajax networking happens asynchronously. It's also possible that different browsers have somewhat different implementations.
Chrome, for example uses a separate process for each browser window which other browsers do not.
来源:https://stackoverflow.com/questions/9998433/do-js-use-non-blocking-i-o-at-os-level-to-support-ajax