Giter VIP home page Giter VIP logo

resolutejs's Introduction

resolute.js

Determined, and unwavering.

Github Releases (by Release) GitHub stars

Finally get to retry during a Promise operation (works in modern browsers as well as node.js), zero dependencies.

It includes a very basic but reliable retry mechanism for a Promise operation.

Minimal requirements: 3kb unminified / 0 dependencies.

Use Cases

  • Database retry
  • API retry
  • Anything...

Packages

NPM - resolutejs

Usage

Download resolute.js from Github Releases (by Release), create a new instance as shown below and optionally provide a callback that is executed every retry, which gives you the current retry count.

Node.js

var Resolute = require("./resolute");

var somePromiseOperation = function() {
    return new Promise(function(resolve, reject) {
        // Make it fail...
        if (err) return reject(err);
        resolve("success");
    });
};

var resolute_options = {
    // Reference to your Promise function, note: this Promise will always fail.
    operation: somePromiseOperation,
    // Maximum number of times to attempt
    maxRetry: 5,
    // Delay between retries in milliseconds
    delay: 2000
};

var resolute_callback = function(retryCount) {
    console.log(retryCount);
};

var resolute = new Resolute(resolute_options, resolute_callback);

// Run the operation stored in options.
resolute.run().then(null).catch(function(err) {
    console.log("failed after trying: " + resolute.maxRetry + " times, with error: " + err);
});

// Pass in a new operation to perform using the same Resolute instance.
resolute.run(somePromiseOperation).then(null).catch(function(err) {
    console.log("failed after trying: " + resolute.maxRetry + " times, with error: " + err);
});

exponential backoff

By passing in the exponential backoff flag resolute will exponentially increase the waiting time between retries.

...

var resolute_options = {
    // Reference to your Promise function, note: this Promise will always fail.
    operation: somePromiseOperation,
    // Maximum number of times to attempt
    maxRetry: 5,
    // Delay between retries in milliseconds
    delay: 2000,
    // exponentially increase wait time between retries
    exponentialBackoff: true, //default false
};

var resolute_callback = function(retryCount, delay) {
    console.log(`Retry ${retryCount} in ${delay} ms`);
};

var resolute = new Resolute(resolute_options, resolute_callback);

// Run the operation stored in options.
resolute.run().then(null).catch(function(err) {
    console.log("failed after trying: " + resolute.maxRetry + " times, with error: " + err);
});

// Pass in a new operation to perform using the same Resolute instance.
resolute.run(somePromiseOperation).then(null).catch(function(err) {
    console.log("failed after trying: " + resolute.maxRetry + " times, with error: " + err);
});

Browser

<script src="resolute.js"></script>
var somePromiseOperation = function() {
    return new Promise(function(resolve, reject) {
        // Make it fail...
        if (err) return reject(err);
        resolve("success");
    });
};

var resolute_options = {
    // Reference to your Promise function, note: this Promise will always fail.
    operation: somePromiseOperation,
    // Maximum number of times to attempt
    maxRetry: 5,
    // Delay between retries in milliseconds
    delay: 2000
};

var resolute_callback = function(retryCount) {
    console.log(retryCount);
};

var resolute = new Resolute(resolute_options, resolute_callback);

// Run the operation stored in options.
resolute.run().then(null).catch(function(err) {
    console.log("failed after trying: " + resolute.maxRetry + " times, with error: " + err);
});

// Pass in a new operation to perform using the same Resolute instance.
resolute.run(somePromiseOperation).then(null).catch(function(err) {
    console.log("failed after trying: " + resolute.maxRetry + " times, with error: " + err);
});

Contributors

resolutejs's People

Contributors

abacaj avatar kovcic avatar m0rganic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

resolutejs's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.