Node JS Asynchronous Loop
August 21, 2020Node JS server is single threaded. Most web frameworks and their servers have multiple threads and the requests are serviced by one thread. If the service has a thread pool of 50, fifty requests are handled all at once. But each thread runs a request from start to finish. The request when it has IO operations, the thread is blocked. So this is left to the OS to manage the resources and the blocked threads do not run and the servicable threads run. Node is asynchronous but single threded. Here there is only one thread running, one thread gets a request, if it has any IO operation it is done async. So we say tell it to do something async and give it a function that will be executed when the async work is finished and move to the next request. This is all handled by a queue. So we have five requests coming. The five request are in the queue. We take the first one. Do some processing on it. Then it needs to do database call. This database is done by some other thread, a system call or something else. I have no idea. We tell it to do the dataabase operation and pass it a function that should run when it finished. So this goes to the end of the queue. We take the next request from the queue from the queue and do its work. Since all the things that needs to happen after it is finished, we can forget about the variables in the previous context. Some closure magic allows the callback to run without problem. There are lot of thing that are done with this callback pattern. The function passing pattern makes it easy to think about. Async/await looks like a straight program. But it is not. It is a very cool thing. Thread blocking and switching seems to be a messy thing. This way they are doing a lot of improvements. It is like saying when you are done with this do this. Telling the steps and sending things away. The callback pattern is used for network communication, file access, long processes and many other things