All Projects β†’ y13i β†’ retryx

y13i / retryx

Licence: MIT license
Promise-based retry workflow library.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to retryx

of
🍬 Promise wrapper with sugar 🍬
Stars: ✭ 13 (-38.1%)
Mutual labels:  promise, error-handling, async-await
Await Of
await wrapper for easier errors handling without try-catch
Stars: ✭ 240 (+1042.86%)
Mutual labels:  promise, error-handling, async-await
Promise Fun
Promise packages, patterns, chat, and tutorials
Stars: ✭ 3,779 (+17895.24%)
Mutual labels:  promise, promise-library, async-await
Await Handler
Basic wrapper for await that allows handling of errors without try/catch blocks
Stars: ✭ 13 (-38.1%)
Mutual labels:  promise, error-handling, async-await
Unityfx.async
Asynchronous operations (promises) for Unity3d.
Stars: ✭ 143 (+580.95%)
Mutual labels:  promise, async-await
Toolkit
Collection of useful patterns
Stars: ✭ 137 (+552.38%)
Mutual labels:  promise, retry
Kitsu
🦊 A simple, lightweight & framework agnostic JSON:API client
Stars: ✭ 166 (+690.48%)
Mutual labels:  promise, async-await
promised-hooks
Middleware utility for your Promises
Stars: ✭ 25 (+19.05%)
Mutual labels:  promise, promise-library
alls
Just another library with the sole purpose of waiting till all promises to complete. Nothing more, Nothing less.
Stars: ✭ 13 (-38.1%)
Mutual labels:  promise, promise-library
buzy
Async queue manager for node and browser
Stars: ✭ 23 (+9.52%)
Mutual labels:  promise, promise-library
co demo
A step-by-step guide about how to avoid callback hell with ES6 Promises + generators (aka make your own "co")
Stars: ✭ 17 (-19.05%)
Mutual labels:  promise, error-handling
Rubico
[a]synchronous functional programming
Stars: ✭ 133 (+533.33%)
Mutual labels:  promise, async-await
nancy
How JavaScript Promise Works
Stars: ✭ 26 (+23.81%)
Mutual labels:  promise, promise-library
auto-async-wrap
automatic async middleware wrapper for expressjs errorhandler.
Stars: ✭ 21 (+0%)
Mutual labels:  error-handling, async-await
Foy
A simple, light-weight and modern task runner for general purpose.
Stars: ✭ 157 (+647.62%)
Mutual labels:  promise, async-await
Jdeferred
Java Deferred/Promise library similar to JQuery.
Stars: ✭ 1,483 (+6961.9%)
Mutual labels:  promise, promise-library
P Queue
Promise queue with concurrency control
Stars: ✭ 1,863 (+8771.43%)
Mutual labels:  promise, npm-package
eslint-config-welly
😎 βš™οΈ ESLint configuration for React projects that I do. Feel free to use this!
Stars: ✭ 21 (+0%)
Mutual labels:  promise, async-await
Emittery
Simple and modern async event emitter
Stars: ✭ 1,146 (+5357.14%)
Mutual labels:  promise, npm-package
P State
Inspect the state of a promise
Stars: ✭ 108 (+414.29%)
Mutual labels:  promise, npm-package

retryx

NPM Version Build Status Coverage Status

About

retryx (ritrΙͺ́ks) is a Promise-based retry workflow library.

Installation

$ npm install --save retryx

Usage

Flowchart

retryx flowchart

API

retryx

retryx(main [, options [, ...args]])

main is a function returns Promise that might be rejected. Required

options is a object contains maxTries, waiter and other hooks. Optional

...args will be passed to main function call. Optional

options

{
  maxTries:       number,
  timeout:        number,
  waiter:         HookFunction,
  retryCondition: HookFunction,
  beforeTry:      HookFunction,
  afterTry:       HookFunction,
  beforeWait:     HookFunction,
  afterWait:      HookFunction,
  doFinally:      HookFunction,
}

HookFunction can receive current try count and last reject reason as arguments. See source.

maxTries

Attempts calling main function specified times or until succeeds.

Set -1 to retry unlimitedly.

default: 5

timeout

Sets the timeout.

Set -1 to no timeout.

default: -1

waiter

Hook function called before each retry. It's meant to return a Promise that resolves after specific duration.

default: exponential backoff. 200ms, 400ms, 800ms, 1600ms and so on.

See default waiter implementation. You can create custom waiter with wait function for shorthand.

retryCondition

Hook function called AFTER each try. If it returns falsy value, retrying will be abandoned even not reaching maxTries.

default: always return true

beforeTry

Hook function called BEFORE each try.

default: nothing to do

afterTry

Hook function called AFTER each try.

default: nothing to do

beforeWait

Hook function called BEFORE each wait.

default: nothing to do

afterWait

Hook function called AFTER each wait.

default: nothing to do

doFinally

Hook function called ONCE whether main function resolved or rejected.

default: nothing to do

Creating an instance

You can create a new instance of retryx with a custom config.

retryx.create(options)

const myRetryx = retryx.create({
  maxTries: 100,
  waiter:   () => new Promise(r => setTimeout(r, 10)),
});

Examples

With AWS SDK

const retryx = require("retryx");
const AWS = require("aws-sdk");

const ec2 = new AWS.EC2();

retryx(() => ec2.describeRegions().promise()).then(response => {
  console.log(response);
});

With axios

const retryx = require("retryx");
const axios = require("axios");

retryx(() => axios.get("http://example.com")).then(response => {
  console.log(response.statusText);
});

With async/await

import retryx from "retryx";

(async () => {
  try {
    const result = await retryx(() => {
      const number = Math.round(Math.random() * 100);

      if (number > 95) {
        return number;
      } else {
        throw number;
      }
    });

    console.log("success", result);
  } catch (n) {
    console.log("fail:", n)
  }
})();

With hooks

const retryx = require("retryx");

retryx(() => {
  const number = Math.round(Math.random() * 100);
  return number > 95 ? Promise.resolve(number) : Promise.reject(number);
}, {
  maxTries:   100,
  beforeWait: (tries) => console.log(`try #${tries} failed. wait 100 ms`),
  waiter:     () => new Promise((r) => setTimeout(r, 100)),
}).then(result => {
  console.log(`success: ${result}`);
});

TypeScript type inference

import retryx from "retryx";

(async () => {
  let result = await retryx(() => 123);
  result = "abc"; // ERROR: Type '"abc"' is not assignable to type 'number'.
})();

TypeScript generics

import retryx from "retryx";

(async () => {
  let result = await retryx<string>(() => { // Explicitly specifies type of promised value to return.
    const number = Math.round(Math.random() * 100);

    if (number < 50) {
      throw new Error();
    } else if (number < 80) {
      return "good";
    } else if (number < 90) {
      return "great";
    } else {
      return number; // ERROR: Type 'number' is not assignable to type 'string | Promise<string>'.
    }
  });
})();

See also

Development

$ git clone
$ cd retryx
$ yarn

Test.

$ yarn test
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].