Reputation: 13
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?
Upvotes: 0
Views: 54
Reputation: 783
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.
Upvotes: 1