简体   繁体   English

多次调用node.js

[英]Multiple calls to node.js

Say I have an API that either (1) runs a loop and adds 1 to a variable initialized at 0 every second, and returns the variable after 3 seconds; 假设我有一个API,要么(1)运行一个循环,然后向每秒初始化为0的变量加1,然后在3秒后返回该变量; or (2) uses setInterval() to add 1 every second and then setTimeout() after 3 seconds to return the variable (after 3 seconds). 或(2)使用setInterval()每秒钟加1,然后在3秒后使用setTimeout()返回变量(3秒后)。 If I run the API twice in sequence, is the first method more inefficient since it'll take 6 seconds to finish running both calls, while the latter will just take 3? 如果我依次运行API两次,第一种方法是否效率较低,因为完成两个调用都需要6秒钟才能完成,而后者只需3秒钟? What are the benefits of doing it in the (1) way? 以(1)方式进行操作有什么好处?

First approach blocks your code execution. 第一种方法阻止您的代码执行。 Forget it as a nightmare! 忘了做噩梦吧! There are no benefits doing it that way. 这样做没有任何好处。

Node.js environment is asynchronous , so stick to it's async nature as much as possible. Node.js环境是异步的 ,因此请尽可能坚持其异步性质。

And yes, please paste some code. 是的,请粘贴一些代码。 Things may vary depending of your actual problem. 根据您的实际问题,情况可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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