Possible solution using a new function that is implemented as process.watchers() , but I donβt know if it is included in the released version. pull request is still open from this post.
But, generally speaking, you should write a custom setTimeout function that adds all timeouts to the counter, and right before the timeouts to the callback it will be deleted from the counter.
Then create a continuous interval and the interval will check and notice when all the walkers are just timeouts set by your user-defined function.
When he notices this, he (will clear all his timeouts, causing) to exit.
This, of course, is not very convenient, because
- The concept is not proven. (I don't know what data will be returned by
process.watchers() , or if it still works yet) - Interval is a polling method. (not nice if you want an immediate return at the same time with low CPU consumption)
George Bailey
source share