Resolute.js – retry JavaScript Promises.

DelilaEllis 8年前

来自: https://github.com/abacaj/resolutejs

resolute.js

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: 1.7kb unminified / 0 dependencies.

Use Cases

  • Database retry
  • API retry
  • Anything...

Packages

None yet.

Usage

Download resolute.js from , 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(err) {    // Simulated promise operation - this will always fail;    return new Promise(function(resolve, reject) {      if (err) return reject(err);      resolve("success");    });  };  var resolute_options = {    // Reference to your Promise function    operation: somePromiseOperation.bind(null, "Will fail."),    // 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);    resolute.run().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(err) {    // Simulated promise operation - this will always fail;    return new Promise(function(resolve, reject) {      if (err) return reject(err);      resolve("success");    });  };  var resolute_options = {    // Reference to your Promise function    operation: somePromiseOperation.bind(null, "Will fail."),    // 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);    resolute.run().then(null).catch(function(err) {    console.log("failed after trying: " + resolute.maxRetry + " times, with error: " + err);  });