Let asynchronous I/O happen by returning control back to the event loop
When a newcomer to Node.js first writes a file reading program, it comes out something like this.
But.. it does not work as intended. The program will never terminate. The while statement creates an infinite loop and halts everything. The file reading will never return its results. Nothing can proceed.
Trying to write the program like in the example is very understandable. In all of the popular web application languages and frameworks it would work. A Java or .NET programmer would expect here to be nothing wrong. In those environments a dedicated thread is allocated for program execution and other threads are easily created as needed to handle tasks elsewhere.
Code is run to completion
To allow other parts of the program to proceed, you have to deliberately give control back. You have to let others use the only available thread. You have to play together with the other parts of the program.
Need to return control back to the system
For the single-threaded model to work a program needs to return back control to "the system". In Node.js this system is called the event loop and similar concept is present in browsers as well.
"Node.js is an asynchronous event driven framework."
(Node.js home page)
Node.js is closely related to events. As the definition suggests it is inherently driven by them.
The event loop is a huge while loop that is started in the node binary. It repeatedly takes incoming events and fires any event listeners subscribed to those events one at a time.
You need to let control back to this event loop. You can do it in several ways. You let control back to the system
- by using
- by using
And you need to do this frequently. The larger the amount of concurrency in your program the more important it is to frequently let other parts of the program to advance as well.
A working example
A correct working example would be simply
and nothing else.
The code sets up the call and returns control to one level up. After that, Node.js will take care of the rest and calls our function when the results are in. At that point we can print the data.
There are 350 000 NPM packages. Your task: pick one.
Learn what to look for in a reliable NPM package.