I have read some intro articles to nodejs and it seems like a fun way of creating webapps. However, i’m not sure if i understand the “only one thread” and “event loop” so i hope someone can clarify for me.
Lets say i have a function “computeBigNumbers(data, callback)”. Now, when a http request is made i call this function and supply a callback for when it’s done.
In the “computeBigNumbers” function, if i don’t specifically run the code in a child process or start a web worker thread, will it be async? I’m thinking no. Correct?
… or call another async function.
But yes, there is only one thread in node.js, and it is your responsibility not to block it (by making time-consuming tasks explicitly asynchronous).
I/O lends itself to callbacks, but if you have a CPU-heavy operation, then you probably need to farm it out to a web worker or child process. An intense loop in Javascript or a synchronous call into a C function will have the whole server wait for you.