0

Could someone help me understand what's happening under the hood here? I'm trying to determine if there's a point at which the below code breaks?

The point is to execute as many function calls as I can as I'm waiting on a response from a remote endpoint that I'm simulating here using setTimeout.

I'm sure there's a limit on how many functions I can invoke this way - the question being, will NodeJS queue them for me, or do I have to chunk them myself? Should I do loops of 1,000 at a time, or 10,000 at a time, etc.?

function theInvocation() {
  return function() {
    setTimeout(() => { console.log('Test!'); }, 2000);
  }
}

(() => {
  for (let ctr = 1; ctr < 1000; ctr++) {
      theInvocation()();
  }
})();
Gary
  • 909
  • 9
  • 26
  • 1
    The delayed functions themselves aren't a problem. You can queue an infinite amount of them, just note that if you queue, say, a thousand to execute after 1s, then the last ones might execute in 2s time. However, the delay itself shouldn't cause a problem. The loop *might*, depending on how many iterations you do at once. Even then, it only blocks other stuff from executing until it finishes, it shouldn't "break the program". Unless it's infinite or you have *extremely* large amount of iterations. – VLAZ Dec 09 '20 at 11:14
  • 2
    At any rate, this seems like [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) - surely there are better ways to handle something than queueing up thousands of invocations for the future. Can you explain what you're actually trying to solve with this? – VLAZ Dec 09 '20 at 11:15
  • The Reverse an IP address to domain name. Using promises delays the result. This is fastest approach I could implement. – Gary Dec 09 '20 at 11:50
  • "*The Reverse an IP address to domain name.*" `nslookup`? And I'm still not clear how or why that requires thousands of queued calls. "*Using promises delays the result.*" promises go on the microtask queue, while timeout delays go on the macrotask queue. There shouldn't be a difference, however, if you flood the microtasks, no macrotask would execute. Depending on your application, this might or might not matter. Still, I don't understand why you create so many tasks in the first place. Neither me, nor anyone else can suggest a potentially better alternative. – VLAZ Dec 09 '20 at 12:11
  • vlaz: i'm asking for the most efficient method of doing ns lookups using nodejs. i need to do potentially a million ip lookups per day so waiting on a promise is too slow, but maybe i'm unfamiliar w/the proper way to do promises in a loop. is there a good example of using a loop w/promises that are the preferred method? – Gary Dec 09 '20 at 12:47
  • "*the most efficient method of doing ns lookups using nodejs*" is a completely different question to what you asked. Had you started with this, you'd probably have had an answer by now. Seems like [`dnsPromises.reverse(ip)`](https://nodejs.org/api/dns.html#dns_dnspromises_reverse_ip) is the easiest way to do a lookup. Given that it's a promise... you treat it as a promise? Either attach a `.then()` or if you have to do a lot of them, you can use `Promise.all`/`Promise.allSettled` to wait for all to finish. – VLAZ Dec 09 '20 at 12:58
  • Hi Vlaz, here's an example of what I'm trying to do: https://stackoverflow.com/questions/65244205/how-do-i-properly-resolve-1-million-domain-names-to-ip-addresses-using-nodejs I fixed it so it updates the screen as opposed to blocking), but this example isn't working well either. What are your thoughts? – Gary Dec 11 '20 at 00:34

0 Answers0