Because everyone loves a tryer! Conditional and repeated function invocation for node and browser.
- Say what?
- What size is it?
- How do I install it?
- How do I use it?
- How do I set up the dev environment?
- What license is it released under?
Sometimes, you want to defer calling a function until a certain pre-requisite condition is met. Other times, you want to call a function repeatedly until some post-requisite condition is satisfied. Occasionally, you might even want to do both for the same function.
To save you writing
on each of those occasions,
a predicate-based approach
that hides the cruft
behind a simple,
Additionally, it allows you to easily specify retry intervals and limits, so that your code doesn't hog the CPU. It also supports exponential backoff of retry intervals, which can be useful when handling indefinite error states such as network failure.
What size is it?
5.6 kb unminified with comments, 1.1 kb minified, 0.5 kb minified + gzipped.
How do I install it?
npm i tryer --save
Or if you just want the git repo:
git clone email@example.com:philbooth/tryer.git
How do I use it?
Loading the library
If you are running in
or another CommonJS-style
tryer like so:
const tryer = ;
It also the supports the AMD-style format preferred by Require.js.
If you are
with an HTML
or neither of the above environments
it will be exported globally as
Calling the exported function
tryer is a function
that can be invoked to
call other functions
conditionally and repeatedly,
without the need for
or loops in your own code.
tryer takes one argument,
an options object
the following properties:
action: The function that you want to invoke. If
actionreturns a promise, iterations will not end until the promise is resolved or rejected. Alternatively,
actionmay take a callback argument,
done, to signal that it is asynchronous. In that case, you are responsible for calling
donewhen the action is finished. If
actionis not set, it defaults to an empty function.
when: A predicate that tests the pre-condition for invoking
whenreturns true (or a truthy value),
actionwill not be called. Defaults to a function that immediately returns
until: A predicate that tests the post-condition for invoking
untilreturns true (or a truthy value),
actionwill no longer be called. Defaults to a function that immediately returns
fail: The error handler. A function that will be called if
limitfalsey values are returned by
until. Defaults to an empty function.
pass: Success handler. A function that will be called after
untilhas returned truthily. Defaults to an empty function.
limit: Failure limit, representing the maximum number of falsey returns from
untilthat will be permitted before invocation is deemed to have failed. A negative number indicates that the attempt should never fail, instead continuing for as long as
untilhave returned truthy values. Defaults to
interval: The retry interval, in milliseconds. A negative number indicates that each subsequent retry should wait for twice the interval from the preceding iteration (i.e. exponential backoff). The default value is
-1000, signifying that the initial retry interval should be one second and that each subsequent attempt should wait for double the length of the previous interval.
// Attempt to insert a database record, waiting until `db.isConnected`// before doing so. The retry interval is 1 second on each iteration// and the call will fail after 10 attempts.;
// Attempt to send an email message, optionally retrying with// exponential backoff starting at 1 second. Continue to make// attempts indefinitely until the call succeeds.let sent = false;;
// Poll a device at 30-second intervals, continuing indefinitely.;
How do I set up the dev environment?
To install the dependencies:
To run the tests:
To lint the code:
npm run lint
To regenerate the minified lib:
npm run minify