Run arbitrary code while waiting for callback in Node?

I'm starting at Node right now. I understand that callbacks prevent Node from blocking while waiting for I / O or some other process, but what does Node do while waiting for I / O to complete? One tutorial that I read on nodejitsu says: "A callback is a function that is called when a given task is completed, which prevents any locks and allows you to run other code in that time." but what other code works? Let, say, another code, this block depends on the information retrieved from the database. Can Node handle other connections while waiting for a callback, or can it run different code in the current block?

0
source share
2 answers

Node works with an event queue. When you run the async operation, this operation is performed independently of the node in the background. node continues to execute the rest of your code in the current thread until it reaches the end of that thread (for example, everything goes back to the top of the stack). As long as node starts the current thread of execution, no other threads of execution will be executed in node (ignoring generators and fibers at the moment). That's why people call it single-threaded. It executes a specific thread of execution one at a time in succession, rather than multiple threads of execution in parallel. Need to finish before the next launch.

, , node . . , , , (, , ). (, , ), node , ( , , , ).

, async ( ), , . node , . - node, , , ...

, nodejs. , nodejs . async , .

, , , nodejs, .

+2

, . -, , . , , node - . , - , , , IO .

0

Source: https://habr.com/ru/post/1610343/


All Articles