0.2.1 • Published 1 year ago

just-retry-it v0.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

just-retry-it

A simple async/await wrapper around promise-retry and retry.

Why?

  • Simplify the interface. You don't need to write the try-catch-retry part yourself.
  • Accept an optional error handler function that will be called after each failed attempt.

Kudos to promise-retry and retry authors 🙏.

Installation

$ npm install just-retry-it

Usage

await retry(operation, options?)
  • operation: The operation to be executed and retried if the execution should fail.
  • options: The options object as it is desrcibed in the retry NPM package.
    • options also supports the errorHandler property (optional) to pass an error handler function, which will be called right after the operation if it throws. The handler receives the thrown error as the input.

Example

import retry from 'just-retry-it';

async function getMessage() {
    if (Math.random() > 0.5) {
        return "Hello World!";
    }

    throw new Error("boom");
}

async function run() {
    // Retry without the error handler.
    const msg1 = await retry(getMessage, { retries: 5 });
    
    async function errorHandler(error) {
        console.log("received an error", error);
    }
    
    // Retry with the error handler.
    const msg2 = await retry(getMessage, { retries: 5, errorHandler });
}

License

MIT

0.2.1

1 year ago

0.2.0

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago