I have been trying to understand in depth how Asynchronous Vs Synchronous code works in JavaScript. Read quite few articles. Understood the concept of Call Stack, Event loop and Function Queue and how each of these three come into action differemtly in case of Async code and Sync code.(https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop https://medium.com/@siddharthac6/javascript-execution-of-synchronous-and-asynchronous-codes-40f3a199e687 ).
But still it does not clear all my doubts.
- JavaScript is single threaded and to handle Async code it uses callback functions, a callback function is function that gets called when a action is completed. How we get to know when that action was completed. Is the action listener a separate process or tread in JavaScript engine?
If our Node JS server gets 10 concurrent request to download a file, and we use Async code to take care of each download request, in this case we will have 10 callback functions that will be triggered when the down load completes. Being single threaded will the download for each client will start after the download for the client ahead of it gets completed or for all the 10 clients it gets started as soon as request is received.
All the articles that I have read on Async code handeling in JavaScript always pick the example of setInterval(), to differentiate between Async and Sync code. I am more interested as how xmlhttprequest works Async.
- Understanding the execution of Async code execution for Javascript is good enough to for Node JS Async code execution also?
Thanks.
APIswhich do make threads for your code under the hood. All depends on how you write your codeforloop).