Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a single thread handle asynchronous code in JavaScript [duplicate]

I know javascript runs on single thread. I also, know when we make an async ajax call using jquery, the code does not stop and continues execution serially. The response is handled by the callback. My question is how does the single thread does this? Does the thread handles the callback & stop the further code execution when we get the response back?

like image 582
Arman Bhuiyan Avatar asked Jul 25 '26 09:07

Arman Bhuiyan


1 Answers

No, it does not stop code execution.

The callback gets queued and when there is nothing else to execute, it will be run.

No two things happen at the same time, and scheduling is not pre-emptive.

So if you keep your single thread busy with things like infinite loops, the callback will never get a chance to run.

like image 91
Thilo Avatar answered Jul 27 '26 23:07

Thilo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!