Usage no npm install needed!

<script type="module">
  import fail4wardRetry from '';



NPM package to implement Retry Pattern in your nodejs applications. Created from this Cookiecutter Template.

CI npm-publish npm-install-and-use-package




Tested on:

  • npm 6.13.4
  • node v10.19.0


npm install --save fail4ward-retry


Import what we need.

import { RetryConfigBuilder, RetryConfig, Retry, UntilLimit } from 'fail4ward-retry';

Set the configuration using the RetryConfigBuilder().

const maxAttempts = 5;
const waitDuration = 1000;

const retryConfig: RetryConfig = new RetryConfigBuilder()

Builder properties we are setting

  • withMaxAttempts() number of attempts to retry.
  • withWaitDuration() backoff time in milliseconds.
  • withStrategy() retry strategy to use. This package currently supports UntilLimit.

Decorate the function that calls your service using the retryConfig instantiated with Retry.decoratePromise().

const retry = Retry.With(retryConfig);
const fn = retry.decoratePromise(failingFn);

Below is an example of the failingFn calls an API. Similar functions can be found in the /example and /__tests__ folders.

Click to see failingFn()
async function failingFn() {
  const url = 'http://localhost:8000/error';
  try {
    const res = await fetch(url);
    const {status} = res;
    if (status === 500) {
      throw new Error('server error');
    return res;
  } catch(e) {
    throw new Error(e);

Call the function that fetches the response from your API and retrieve the response.

try {
        const res = await fn();
        const retryResponse = await res.json();
        console.log('retryResponse: ', retryResponse);
} catch(e) {

Run the example

  1. Checkout the repo

     git clone
  2. Change directory to the example folder

     cd example/
  3. Install dependencies

     npm install
  4. Run a test service

     docker run --name fail-svc -p 8000:8000 -it ardydedase/fail4ward:latest
  5. Run the example file

     npm run dev


  1. Checkout the repo

     git clone
  2. Install dependencies

     npm install
  3. Run build. This will generate the compiled code with type definitions in the dist folder.

     npm run build
  4. Formatting and linting.

     npm run lint
     npm run format
  5. Run tests

     npm test