0

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?

1 Answer 1

1

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?

... 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.

Sign up to request clarification or add additional context in comments.

3 Comments

Also, if i wanted my node application to behave like a traditional server i could just start a new child process with every request, right?
That would defeat the purpose of node, though. If it is I/O heavy, a single thread can handle lots of concurrent requests. When you write for node, you already pay the mental cost of all these callbacks, that investment would be wasted.
It was just a thought experiment, to see if i understood it correctly, i'm not going to do that:)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.