wait-for-throwable

Simple utility to retry an erroring function until it succeeds

Usage no npm install needed!

<script type="module">
  import waitForThrowable from 'https://cdn.skypack.dev/wait-for-throwable';
</script>

README

wait-for-throwable

travis npm-downloads npm-version

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 (or async) 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 with count, retries will stop at whichever value is reached first.
    • [count = Infinity] {Number}: the maximum number of times to retry. If this is used along with total, retries will stop at whichever value is reached first.