Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If javascript is single threaded then how ajax calls are manage?

Tags:

javascript

I have learned that, JavaScript is always single-threaded. If you're executing a JavaScript block of code on a page then no other JavaScript on that page will currently be executed. If this is the case then how Javascript manage Ajax call? If a single thread is busy and currently executing other Javascript on that page then how thread manage when call return(success or otherwise) from ajax? Is Javascript internally uses more than a thread to handle call return?

like image 885
sachin soni Avatar asked Jan 13 '20 18:01

sachin soni


3 Answers

The javascript engine itself is single threaded, but javascript executes in a hosting environment that includes more than just the js engine. A hosting environment would be something such as a browser, a server, etc.

Within the hosting environment is something called an event loop. This event loop manages when certain parts of a js script should run. So when you write something such as:

setTimeout(() => console.log('tick'), 1000)

the callback function gets put on the event loop (not quite, but close enough for explanation purposes) and once the current js execution thread is empty the event loop will check if there is anything in the queue and if there is it will push it onto the main js execution thread.

So in summary, the js engine itself is single threaded but there is a lot more that goes into a full js hosting environment that makes it work.

like image 164
Travis James Avatar answered Oct 22 '22 00:10

Travis James


JavaScript is single-threaded with respect to the code that is executed within one context. So in a JavaScrip Engine code of the same context cannot run in parallel. You can have multiple contexts running in parallel (like WebWorkers), but different contexts cannot share objects (or only in a clearly define and limited way).

The IO is implemented with an event-based approach, which means, that out of the perspective of the JavaScript side, data is pushed to or pulled from a buffer. How that buffer is filled/emptied is not in the JavaScript Land anymore, and at that point, the engine could use threads, or continue to use an event-based approach like libuv (which is used by V8 the js engine of chrome and nodejs) provides. libuv or the system then itself would do something with the data, which again could either be done with threads or event-based.

So, in theory, the IO could be done completely without threads.

like image 2
t.niese Avatar answered Oct 21 '22 22:10

t.niese


JavaScript is actually single threaded because every JavaScript code will run in a single thread (that's not true anymore in the modern web because Web workers allow JavaScript code to run in separate threads, though). That means there is a single callback queue, and the callbacks run when the main thread is not busy, and thus can potentially be delayed. Also, note that the UI (web page) is blocked while JavaScript runs, preventing the user from doing anything.

All of the browser's internal mechanisms such as XMLHttpRequest are not JavaScript, only the API is (send(), open(), onreadystatechange, etc.), so it's up to the browser's editor to make use of other threads for these internal mechanisms, as long as the API complies with the W3C specification.

like image 1
Guerric P Avatar answered Oct 21 '22 23:10

Guerric P