Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a lodash function to wait for a condition?

Tags:

node.js

lodash

I'm using an API which provides an async method used like this:

api.isActive(function(err, active) {
    // check for error
    // use active boolean
});

The underlying object state is "volatile", i.e. it changes without any interaction from the program.

I'd like to wait for a given state - this will involve calling isActive repeatedly (with some interval) until the desired state is determined. The entire operation should be given a maximum amount of time for the state to be achieved before bailing out. An old-school nodejs callback(err) should be called with the completion (this can be promise-ified later).

I could write some code with setTimeout() but I'd prefer to use a canned solution or pattern, and lodash seems likely to provide something but I can't quite see what method could be used for this.

like image 822
dmansfield Avatar asked Feb 16 '26 03:02

dmansfield


1 Answers

lodash doesn't have such function, cause it's can be done with setInterval easily

if you want some wait logic, try to look on https://github.com/luciotato/waitfor

but, anyway, you need to use setInterval / setTimeout for checks

like image 182
vmkcom Avatar answered Feb 18 '26 16:02

vmkcom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!