无法理解NodeJS中的一些基本逻辑

时间:2022-01-14 20:53:16

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 (P.S-It is the concept what i understood...i may b wrong), then why we r callng nodejs as single threaded program.

下面的代码是nodeJS代码。我是nodeJS的新手,我对nodejs中的代码流非常困惑,因为它告诉nodejs是单线程的,而另一方面它也告诉我们如果我没有错误,回调和IO是异步的。任何人都可以告诉我回调的实际含义以及代码是如何工作的。是这样我们作为回调调用的异步函数是由一些其他线程/进程执行而不是由单个nodejs线程执行(PS-这是我理解的概念......我可能是错的),那么为什么我们将callng nodejs作为单线程程序。

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);

2 个解决方案

#1


1  

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.

Nodejs是单线程的。有一个连续运行的事件循环,并执行它必须执行的任何指令。所以基本上当你使用5秒间隔的setTimeout函数时,它会在5秒后放置一些代码,由事件循环执行。当然,如果事件循环忙于执行其他代码,那么它将推迟执行代码以用于后续阶段。所以它可能不会在5秒后执行,而是在5.1秒后执行。

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

所以当你调用setTimeout(function(){...},5000);您正在安排一些javascript代码,以便在事件循环后至少5秒钟执行。

#2


1  

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.

因此,您不会同时运行两个传递函数。如果你在交付内部睡觉,其他回调将不得不等待。这就是为什么在这样的单线程环境中,重要的是快速完成工作并返回(到浏览器或节点),以便事件循环可以继续监听同一线程上的更多回调。

#1


1  

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.

Nodejs是单线程的。有一个连续运行的事件循环,并执行它必须执行的任何指令。所以基本上当你使用5秒间隔的setTimeout函数时,它会在5秒后放置一些代码,由事件循环执行。当然,如果事件循环忙于执行其他代码,那么它将推迟执行代码以用于后续阶段。所以它可能不会在5秒后执行,而是在5.1秒后执行。

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

所以当你调用setTimeout(function(){...},5000);您正在安排一些javascript代码,以便在事件循环后至少5秒钟执行。

#2


1  

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.

因此,您不会同时运行两个传递函数。如果你在交付内部睡觉,其他回调将不得不等待。这就是为什么在这样的单线程环境中,重要的是快速完成工作并返回(到浏览器或节点),以便事件循环可以继续监听同一线程上的更多回调。