wait-for-throwable
v1.0.1
Published
Simple utility to retry an erroring function until it succeeds
Downloads
8,630
Maintainers
Readme
wait-for-throwable
This method was inspired by built-in wait utilities in test frameworks, such as waitFor
in testing-library, waitUntil
in webdriverIO, or waitFor
in puppeteer. However, this module uses a standalone and generic implementation, allowing you to wait for and retry any function, both synchronous or Promise-based. You can use this in your tests directly with your favorite assertion helper.
Install
npm install wait-for-throwable
API
waitForThrowable()
→ Promise
Retries the provided method until it succeeds. The method is executed immediately, and if it fails, is retries at a defined internal until the total amount of wait time is reached. If the method succeeds at any time during the retries, the promise will resolve with the resulting value of the method. If the method continues to fail when the total timeout is reached, no more retries will occur and the promise will be rejected with the latest failure.
The arguments are:
method {Function}
: the method to retrym which can be a synchronous method or a promise-returning (orasync
) method[options] {Object}
: options that define the behavior of the retries. Everything is optional.[interval = 5] {Number}
: the amount of time to wait between retries[total = 2000] {Number}
: the total amount of time to retry. If this is used along withcount
, retries will stop at whichever value is reached first.[count = Infinity] {Number}
: the maximum number of times to retry. If this is used along withtotal
, retries will stop at whichever value is reached first.