简体   繁体   中英

Can't understand some basic logic in NodeJS

The code below is a nodeJS code. I am new to nodeJS and i am pretty confused with the flow of code in a nodejs since it tells that nodejs is single threaded and in the other hand it also tells us that the callbacks and IO are asynchronous if i am not wrong. Can anyone give me the actual meaning of callbacks and how the code is working. Is it so that the asynchronous function which we are calling as callbacks are executed by some other thread/process and not by the single nodejs thread (PS-It is the concept what i understood...i may b wrong), then why we r callng nodejs as single threaded program.

function placeOrder(orderNo) {


    setTimeout(function() {
    deliver(orderNo); 
}, 5000);
    console.log("Order is: " + orderNo);

}

function deliver(orderNo) {
    console.log("Item is delivered with Order No.- " + orderNo);
}

placeOrder(1);
placeOrder(2);
placeOrder(3);
placeOrder(4);
placeOrder(5);
placeOrder(6);

Nodejs is single threaded. There's an event loop that runs continuously and executes whatever instructions it has to execute. So basically when you use the setTimeout function with 5 seconds interval, it will place some code to be executed by the event loop 5 seconds later. Of course if the event loop is busy executing some other code at this time it will postpone the execution of your code for a later stage. So it might not execute 5 seconds later but rather 5.1 seconds later.

So when you call setTimeout(function() { ... }, 5000); you are scheduling some javascript code to execute at least 5 seconds later by the event loop.

Asynchronous is not the same as multi-threaded. In the case above, you're asking for async callbacks, but your single thread listens for those callbacks when it's not running your other code. The callbacks all come back to the same thread that requested them.

You won't get two deliver functions running at the same time because of this. If you sleep inside of deliver , the other callbacks will have to wait. This is why, in single-threaded environments like this, it is important to get the work done fast and return (to the browser or to node) so that the event loop can resume listening for more callbacks on the same thread.

Asynchronous in Nodejs is a definition for general idea of how Nodejs based on. For details, nodejs is using single thread to run the event loop for handling request (to specify which one is non-blocking IO and which one is blocking IO)

In case the request is non-blocking IO actions. It will be attached callback handler for quickly response. The computation of function, inside function will be handle from the logic of Stack & Heap .

In case the request is blocking IO actions. Nodejs will get one thread of thread pool to handle that request and the program itself will have something called IO waiting time .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM