pax_global_header00006660000000000000000000000064127560314760014525gustar00rootroot0000000000000052 comment=4f07750e903f819bcc38622fa53deb7162f5e978 node-promise-retry-1.1.1/000077500000000000000000000000001275603147600152715ustar00rootroot00000000000000node-promise-retry-1.1.1/.editorconfig000066400000000000000000000003341275603147600177460ustar00rootroot00000000000000root = true [*] indent_style = space indent_size = 4 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.md] trim_trailing_whitespace = false [package.json] indent_size = 2 node-promise-retry-1.1.1/.gitignore000066400000000000000000000000311275603147600172530ustar00rootroot00000000000000node_modules npm-debug.* node-promise-retry-1.1.1/.jshintrc000066400000000000000000000022411275603147600171150ustar00rootroot00000000000000{ "predef": [ "console", "require", "define", "describe", "it", "before", "beforeEach", "after", "afterEach", "Promise" ], "node": true, "devel": true, "bitwise": true, "curly": true, "eqeqeq": true, "forin": false, "immed": true, "latedef": false, "newcap": true, "noarg": true, "noempty": false, "nonew": true, "plusplus": false, "regexp": true, "undef": true, "unused": true, "quotmark": "single", "strict": true, "trailing": true, "asi": false, "boss": false, "debug": false, "eqnull": true, "es5": false, "esnext": false, "evil": false, "expr": true, "funcscope": false, "globalstrict": false, "iterator": false, "lastsemic": false, "laxbreak": false, "laxcomma": false, "loopfunc": true, "multistr": false, "onecase": true, "regexdash": false, "scripturl": false, "smarttabs": false, "shadow": false, "sub": false, "supernew": false, "validthis": false, "nomen": false, "onevar": false, "white": true } node-promise-retry-1.1.1/.travis.yml000066400000000000000000000000661275603147600174040ustar00rootroot00000000000000language: node_js node_js: - "0.12" - "4" - "5" node-promise-retry-1.1.1/LICENSE000066400000000000000000000020401275603147600162720ustar00rootroot00000000000000Copyright (c) 2014 IndigoUnited Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. node-promise-retry-1.1.1/README.md000066400000000000000000000063011275603147600165500ustar00rootroot00000000000000# node-promise-retry [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url] [npm-url]:https://npmjs.org/package/promise-retry [downloads-image]:http://img.shields.io/npm/dm/promise-retry.svg [npm-image]:http://img.shields.io/npm/v/promise-retry.svg [travis-url]:https://travis-ci.org/IndigoUnited/node-promise-retry [travis-image]:http://img.shields.io/travis/IndigoUnited/node-promise-retry/master.svg [david-dm-url]:https://david-dm.org/IndigoUnited/node-promise-retry [david-dm-image]:https://img.shields.io/david/IndigoUnited/node-promise-retry.svg [david-dm-dev-url]:https://david-dm.org/IndigoUnited/node-promise-retry#info=devDependencies [david-dm-dev-image]:https://img.shields.io/david/dev/IndigoUnited/node-promise-retry.svg Retries a function that returns a promise, leveraging the power of the [retry](https://github.com/tim-kos/node-retry) module to the promises world. There's already some modules that are able to retry functions that return promises but they were rather difficult to use or do not offer an easy way to do conditional retries. ## Installation `$ npm install promise-retry` ## Usage ### promiseRetry(fn, [options]) Calls `fn` until the returned promise ends up fulfilled or rejected with an error different than a `retry` error. The `options` argument is an object which maps to the [retry](https://github.com/tim-kos/node-retry) module options: - `retries`: The maximum amount of times to retry the operation. Default is `10`. - `factor`: The exponential factor to use. Default is `2`. - `minTimeout`: The number of milliseconds before starting the first retry. Default is `1000`. - `maxTimeout`: The maximum number of milliseconds between two retries. Default is `Infinity`. - `randomize`: Randomizes the timeouts by multiplying with a factor between `1` to `2`. Default is `false`. The `fn` function will receive a `retry` function as its first argument that should be called with an error whenever you want to retry `fn`. The `retry` function will always throw an error. If there's retries left, it will throw a special `retry` error that will be handled internally to call `fn` again. If there's no retries left, it will throw the actual error passed to it. If you prefer, you can pass the options first using the alternative function signature `promiseRetry([options], fn)`. ## Example ```js var promiseRetry = require('promise-retry'); // Simple example promiseRetry(function (retry, number) { console.log('attempt number', number); return doSomething() .catch(retry); }) .then(function (value) { // .. }, function (err) { // .. }); // Conditional example promiseRetry(function (retry, number) { console.log('attempt number', number); return doSomething() .catch(function (err) { if (err.code === 'ETIMEDOUT') { retry(err); } throw err; }); }) .then(function (value) { // .. }, function (err) { // .. }); ``` ## Tests `$ npm test` ## License Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php). node-promise-retry-1.1.1/index.js000066400000000000000000000025271275603147600167440ustar00rootroot00000000000000'use strict'; var errcode = require('err-code'); var retry = require('retry'); var hasOwn = Object.prototype.hasOwnProperty; function isRetryError(err) { return err && err.code === 'EPROMISERETRY' && hasOwn.call(err, 'retried'); } function promiseRetry(fn, options) { var temp; var operation; if (typeof fn === 'object' && typeof options === 'function') { // Swap options and fn when using alternate signature (options, fn) temp = options; options = fn; fn = temp; } operation = retry.operation(options); return new Promise(function (resolve, reject) { operation.attempt(function (number) { Promise.resolve() .then(function () { return fn(function (err) { if (isRetryError(err)) { err = err.retried; } throw errcode('Retrying', 'EPROMISERETRY', { retried: err }); }, number); }) .then(resolve, function (err) { if (isRetryError(err)) { err = err.retried; if (operation.retry(err || new Error())) { return; } } reject(err); }); }); }); } module.exports = promiseRetry; node-promise-retry-1.1.1/package.json000066400000000000000000000015151275603147600175610ustar00rootroot00000000000000{ "name": "promise-retry", "version": "1.1.1", "description": "Retries a function that returns a promise, leveraging the power of the retry module.", "main": "index.js", "scripts": { "test": "mocha --bail -t 10000" }, "bugs": { "url": "https://github.com/IndigoUnited/node-promise-retry/issues/" }, "repository": { "type": "git", "url": "git://github.com/IndigoUnited/node-promise-retry.git" }, "keywords": [ "retry", "promise", "backoff", "repeat", "replay" ], "author": "IndigoUnited (http://indigounited.com)", "license": "MIT", "devDependencies": { "expect.js": "^0.3.1", "mocha": "^3.0.0", "sleep-promise": "^2.0.0" }, "dependencies": { "err-code": "^1.0.0", "retry": "^0.10.0" }, "engines": { "node": ">=0.12" } } node-promise-retry-1.1.1/test/000077500000000000000000000000001275603147600162505ustar00rootroot00000000000000node-promise-retry-1.1.1/test/test.js000066400000000000000000000163311275603147600175710ustar00rootroot00000000000000'use strict'; var expect = require('expect.js'); var promiseRetry = require('../'); var promiseDelay = require('sleep-promise'); describe('promise-retry', function () { it('should call fn again if retry was called', function () { var count = 0; return promiseRetry(function (retry) { count += 1; return promiseDelay(10) .then(function () { if (count <= 2) { retry(new Error('foo')); } return 'final'; }); }, { factor: 1 }) .then(function (value) { expect(value).to.be('final'); expect(count).to.be(3); }, function () { throw new Error('should not fail'); }); }); it('should call fn with the attempt number', function () { var count = 0; return promiseRetry(function (retry, number) { count += 1; expect(count).to.equal(number); return promiseDelay(10) .then(function () { if (count <= 2) { retry(new Error('foo')); } return 'final'; }); }, { factor: 1 }) .then(function (value) { expect(value).to.be('final'); expect(count).to.be(3); }, function () { throw new Error('should not fail'); }); }); it('should not retry on fulfillment if retry was not called', function () { var count = 0; return promiseRetry(function () { count += 1; return promiseDelay(10) .then(function () { return 'final'; }); }) .then(function (value) { expect(value).to.be('final'); expect(count).to.be(1); }, function () { throw new Error('should not fail'); }); }); it('should not retry on rejection if retry was not called', function () { var count = 0; return promiseRetry(function () { count += 1; return promiseDelay(10) .then(function () { throw new Error('foo'); }); }) .then(function () { throw new Error('should not succeed'); }, function (err) { expect(err.message).to.be('foo'); expect(count).to.be(1); }); }); it('should not retry on rejection if nr of retries is 0', function () { var count = 0; return promiseRetry(function (retry) { count += 1; return promiseDelay(10) .then(function () { throw new Error('foo'); }) .catch(retry); }, { retries : 0 }) .then(function () { throw new Error('should not succeed'); }, function (err) { expect(err.message).to.be('foo'); expect(count).to.be(1); }); }); it('should reject the promise if the retries were exceeded', function () { var count = 0; return promiseRetry(function (retry) { count += 1; return promiseDelay(10) .then(function () { throw new Error('foo'); }) .catch(retry); }, { retries: 2, factor: 1 }) .then(function () { throw new Error('should not succeed'); }, function (err) { expect(err.message).to.be('foo'); expect(count).to.be(3); }); }); it('should pass options to the underlying retry module', function () { var count = 0; return promiseRetry(function (retry) { return promiseDelay(10) .then(function () { if (count < 2) { count += 1; retry(new Error('foo')); } return 'final'; }); }, { retries: 1, factor: 1 }) .then(function () { throw new Error('should not succeed'); }, function (err) { expect(err.message).to.be('foo'); }); }); it('should convert direct fulfillments into promises', function () { return promiseRetry(function () { return 'final'; }, { factor: 1 }) .then(function (value) { expect(value).to.be('final'); }, function () { throw new Error('should not fail'); }); }); it('should convert direct rejections into promises', function () { promiseRetry(function () { throw new Error('foo'); }, { retries: 1, factor: 1 }) .then(function () { throw new Error('should not succeed'); }, function (err) { expect(err.message).to.be('foo'); }); }); it('should not crash on undefined rejections', function () { return promiseRetry(function () { throw undefined; }, { retries: 1, factor: 1 }) .then(function () { throw new Error('should not succeed'); }, function (err) { expect(err).to.be(undefined); }) .then(function () { return promiseRetry(function (retry) { retry(); }, { retries: 1, factor: 1 }); }) .then(function () { throw new Error('should not succeed'); }, function (err) { expect(err).to.be(undefined); }); }); it('should retry if retry() was called with undefined', function () { var count = 0; return promiseRetry(function (retry) { count += 1; return promiseDelay(10) .then(function () { if (count <= 2) { retry(); } return 'final'; }); }, { factor: 1 }) .then(function (value) { expect(value).to.be('final'); expect(count).to.be(3); }, function () { throw new Error('should not fail'); }); }); it('should work with several retries in the same chain', function () { var count = 0; return promiseRetry(function (retry) { count += 1; return promiseDelay(10) .then(function () { retry(new Error('foo')); }) .catch(function (err) { retry(err); }); }, { retries: 1, factor: 1 }) .then(function () { throw new Error('should not succeed'); }, function (err) { expect(err.message).to.be('foo'); expect(count).to.be(2); }); }); it('should allow options to be passed first', function () { var count = 0; return promiseRetry({ factor: 1 }, function (retry) { count += 1; return promiseDelay(10) .then(function () { if (count <= 2) { retry(new Error('foo')); } return 'final'; }); }).then(function (value) { expect(value).to.be('final'); expect(count).to.be(3); }, function () { throw new Error('should not fail'); }); }); });