简体   繁体   中英

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; or (2) uses setInterval() to add 1 every second and then setTimeout() after 3 seconds to return the variable (after 3 seconds). 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? What are the benefits of doing it in the (1) way?

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.

And yes, please paste some code. Things may vary depending of your actual problem.

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