2

Got a question here.

I have a component that needs to poll an api every 2 seconds.

To achieve the "wait to seconds part" I have a very simple util function that you call with the amount of time to sleep and will return a promise that resolves in the specified amount of time. With this util, my component can just do await sleep(2000) on a while loop to poll the api.

My question is, what happens if the component gets destroyed while it is waiting for the promise to resolve? Would the promise resolve and continue the execution of the method? Would the promise just be orphaned and once it resolves it will be a no-op? Would this somehow cause a memory leak?

Please let me know if I need to provide more details.

  • A promise will continue, there is no built in way to cancel promises. You won't get any memory leaks, but depending what your doing after the 2 seconds, might cause logic errors. – Keith Oct 31 '18 at 22:28
  • Possible duplicate of [Promise - is it possible to force cancel a promise](https://stackoverflow.com/questions/30233302/promise-is-it-possible-to-force-cancel-a-promise) – Joe Clay Oct 31 '18 at 22:28
  • You might want to use `setInterval` for polling. – Roy J Oct 31 '18 at 23:22
  • 1
    The problem with `setInterval` is that the callback may take longer than the interval, since I'm performing AJAX calls, I want to schedule the next interval until the callback has returned. – Hector Romero Granillo Nov 02 '18 at 00:18

0 Answers0