util-retry

A thin and lightweight wrapper for calling async function no more than specific times before returning an error

Usage no npm install needed!

<script type="module">
  import utilRetry from 'https://cdn.skypack.dev/util-retry';
</script>

README

A thin and lightweight utilty to wrap an async function and makes it retryable. If all retires failed, the last attampt error will be sent back.

Example #1

const retry = require('util-retry');

var request = retry(api); // try calling api function 3 times

request({...options as required by the api}, callback );

Other Examples

const retry = require('util-retry');

// try calling api function 5 times
var request = retry(5, api);

// same as above
var request = retry({times: 5}, api);

// set 10 seconds to wait between retries.
var request = retry({times: 5, wait: 10000}, api);

retry([options], task)

  • options: <Number>|<Object>

    • times: Unsigned int. Retry limitation. Default: 3.
    • wait : Milliseconds. The interval time to wait between retries. Default: 0

    If options is a number, it means times

  • task

    Normal async function, nodejs style callback is always the last argument.