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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With