2019-09-11 admin


什么是re,Do it again, after a bit.



Do it again, if it doesn’t work the first time. Supports various configurable retry strategies, including: constant, exponential backoff and linear backoff.

Functions are styled to match the simplicity and ease of use found in the async library.


npm install re

Quick Example

var Re = require('re'),
    re = new Re();

re.try(repeatMe, doMeAtTheEnd);

var repeatMe = function(retryCount, done){
    if(retryCount < 2) done(new Error("Not there yet!"));
    else done(null, retryCount);

var doMeAtTheEnd = function(err, retryCount){
    console.log("It took this many tries: " + retryCount);

In the Browser

Tested in recent versions of Internet Explorer, Firefox and Chrome. Usage:

<script type="text/javascript" src="re.js"></script>
<script type="text/javascript">
  var re = new Re();

  re.try(repeatMe, doMeAtTheEnd);

  // repeatMe and doMeAtTheEnd are exactly as above


Try it in your browser with this test: test/test.html or play with the test in this fiddle: re-fiddle (these pages don’t work in IE, because it’s recently gone from lax to pedantic).


If you like the defaults, call it like this:

var Re = require('re'),
    re = new Re();

re.try(function(retryCount, done){
    if(retryCount < 2) done(new Error("Not there yet!"));
    else done(null, retryCount);
  function(err, retryCount){
    console.log("It took this many retries: " + retryCount);

The re.try function takes two arguments, a function to call until it works (or we run out of retries) and a function to call when it finally succeeds (or we fail too many times). As the name suggests we automatically wrap your function in a standard try block and, if an exception occurs, call it again according to the retry schedule.

This first function passed to re.try should take 2 arguments like this:

function operation(retryCount, done)

The retryCount argument is the number of the current retry. It’ll be zero the first time and get bigger every time.

The done argument is a function to call when you’ve completed your operation. If you encounter an error condition, pass in the err object as the first argument. If you don’t encounter an error, pass in a falsy first argument (null works). If you give us a falsy error and no exception happens, we call your callback with all the arguments passed into this function.

The second function passed to re.try can take as many arguments as you like but should always start with an error parameter. This will be falsy, if no error happens.

The function is like re.try except it doesn’t wrap your operation in a try...catch.


The default options look like this:

var options = {
    retries : 10,
    strategy : {

You pass this options object into the Re constructor.

var Re = require('re'),
    re = new Re(options);

This gives you 10 retries and an exponential backoff strategy with the following progression (in milliseconds): 100, 200, 400, 800, 1600, 3200, 6400, 12800, 25600, 51200

Retry Strategy Examples

The following will retry every 400 milliseconds:

{"type": Re.STRATEGIES.CONSTANT, "initial": 400}

The following will give a linear backoff strategy that has the following progression (when paired with retries: 10) : 200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 1800

{"type": Re.STRATEGIES.LINEAR, "initial": 200, "max":1800}

Both progressive strategies accept the max option. All strategies also accept a rand option. This is a Boolean that adds a random multiplier between 1 and 2. This makes them act like the tradition backoff function. This option is set to false by default.


Test coverage is good and expanding. We use mocha.

Technical Details

The traditional exponential backoff function is described here: Exponential Backoff in Distributed Systems. This is equivalent to our exponential backoff function with the rand option set to true.

Our formula for exponential backoff looks something like this, when using all the options:

return Math.min(random * initial * Math.pow(base, retry), max);

Where random is a random number in the half-open interval [1, 2). When randomness is turned off, the value of this variable is always 1.

If you don’t specify the max option, the formula looks like this:

return random * initial * Math.pow(base, retry);

I’m shamelessly stealing the following link from node-retry just because it’s fun for nerdy math people to play with. You can use it to calculate the exact value you need for the base option so that all retry intervals sum to a desired amount: Wolfram Alpha.


转载请注明:文章转载自 JavaScript中文网 []



Facebook发布React Native!
React.js Conf 2015会议上,Facebook发布了React Native。 React.js 是 Facebook 推出的一个用来构建用户界面的 JavaScript 库。 React中,把一切东西都看成组件,而且所有组件...
React Native 用JavaScript编写原生ios应用
ReactNative 可以基于目前大热的开源JavaScript库React.js来开发iOS和Android原生App。而且React Native已经用于生产环境——Facebook Groups iOS 应用就是基于它开发的。 Re...
request-promise是什么 什么是request-promise,The simplified HTTP request client 'request' with Promise support. Powered by Blue...
jQuery插件Slider Revolution实现响应动画滑动图片切换效果
在 Microsoft Azure 中使用 MEAN 堆栈基于开放数据协议
网络开发人员通常构建伟大的应用程序在客户端使用JavaScript和ASP(c#或Visual Basic . NET)在服务器端。 但是如果你能使用一个共同的语言来构建应用程序的所有层堆栈,从浏览器和服务器端业务处理服务层,甚至在数据库查...
FreeMarker与Web容器无关,即在Web运行时,它并不知道Servlet或HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成XML,JSP或Java 等。 freemarker中显示某对象使用${name}. 但如果nam...
Easyui Tree获取当前选择节点的所有顶级父节点
只支持四层目录结构,比较笨的一个方法 JS代码 var node = $(&#x27;#tree&#x27;).tree(&#x27;getSelected&#x27;); &#x2F;&#x2F;获取该节点所有父节点 ...
vscode 默认配置对于 react 的 JSX 语法不友好,体现在使用自动格式化或者粘贴后默认缩进错误,尽管可以通过改变 language mode 缓解错误,但更改 language mode 后的格式化依然不够理想。 通过搭配使用 ...
AngularJS是一款非常强大的前端MVC框架。同时,它也引入了相当多的概念,这些概念我们可能不是太熟悉。(译者注:老外真谦虚,我大天朝的码农对这些概念那是相当熟悉啊!)这些概念有: Directive(指令) Controller(控制...