diff --git a/src/retry.js b/src/retry.js index be55a77..83bb9c3 100644 --- a/src/retry.js +++ b/src/retry.js @@ -2,33 +2,35 @@ var _ = require("./_"); var sleep = require("./sleep"); var $retryError = {}; -module.exports = function (retries, span, fn, self) { - var errs = [], args; +module.exports = function (initRetries, span, fn, self) { + return function () { + var retries = initRetries; + var errs = [], args = arguments; - if (_.isFunction(span)) { - self = fn; - fn = span; - span = 0; - } + if (_.isFunction(span)) { + self = fn; + fn = span; + span = 0; + } - var countdown = _.isFunction(retries) ? - retries : function () { return sleep(span, retries--); }; + var countdown = _.isFunction(retries) ? + retries : function () { return sleep(span, retries--); }; - function tryFn (isContinue) { - return isContinue ? fn.apply(self, args) : _.Promise.reject($retryError); - } + function tryFn (isContinue) { + return isContinue ? fn.apply(self, args) : _.Promise.reject($retryError); + } - function onError (err) { - if (err === $retryError) return _.Promise.reject(errs); + function onError (err) { + if (err === $retryError) return _.Promise.reject(errs); - errs.push(err); - return attempt(); - } + errs.push(err); + return attempt(); + } - function attempt () { - if (args === void 0) args = arguments; - return _.Promise.resolve(countdown(errs)).then(tryFn).catch(onError); - } + function attempt () { + return _.Promise.resolve(countdown(errs)).then(tryFn).catch(onError); + } - return attempt; + return attempt(); + }; }; diff --git a/test/lab.js b/test/lab.js index 5c8f859..d69098d 100644 --- a/test/lab.js +++ b/test/lab.js @@ -1,112 +1,13 @@ /*eslint-disable */ -// require('core-js') +var Promise = Yaku = require("../src/yaku"); +var utils = require("../src/utils") -// var Promise = require("../src/yaku"); -var Promise = require("my-promise").Promise; -var setPrototypeOf = require("setprototypeof"); -var Symbol = global.Symbol || {}; - -if (!Symbol.species) - Symbol.species = 'Symbol(species)'; - -function sleep (time) { - return new Promise(function (resolve) { - setTimeout(resolve, time); - }); -} - - // function BadResolverPromise(executor) { - // var p = new Promise(executor); - // executor(3, function () {}); - - // this.then = p.then; - // this.constructor = BadResolverPromise; - // } - // BadResolverPromise.prototype = Promise.prototype; - // BadResolverPromise.all = Promise.all; - // BadResolverPromise.race = Promise.race; - // BadResolverPromise.reject = Promise.reject; - // BadResolverPromise.resolve = Promise.resolve; - - // new BadResolverPromise(function () {}).then(function () {}); - - - - - - // var promise = new Promise(function (it){ it(42); }); - - // promise.constructor = function () { }; - - // return promise.then(function () { - // console.log("ok"); - // }); - - - - - - - // var promise, FakePromise1, FakePromise2; - // promise = new Promise(function (it){ it(42); }); - - // promise.constructor = FakePromise1 = function a (it){ - // it(function () {}, function () {}); - // }; - - // FakePromise1[Symbol.species] = FakePromise2 = function b (it){ - // it(function () {}, function () {}); - // }; - // setPrototypeOf(FakePromise2, Promise); - // return console.log( - // promise.then(function () {}) instanceof FakePromise2 - // ); - - - - - - - - function SubPromise (it) { - var self; - self = new Promise(it); - setPrototypeOf(self, SubPromise.prototype); - self.mine = "subclass"; - return self; - } - - var result = []; - - setPrototypeOf(SubPromise, Promise); - SubPromise.prototype = Object.create(Promise.prototype); - SubPromise.prototype.constructor = SubPromise; - - var p1 = SubPromise.resolve(5); - result.push(p1.mine); - p1 = p1.then(function (it) { - return result.push(it); - }); - result.push(p1.mine); - - var p2 = new SubPromise(function (it) { - return it(6); - }); - result.push(p2.mine); - p2 = p2.then(function (it) { - return result.push(it); + var fn = utils.retry(2, 10, function (v) { + return v; }); - result.push(p2.mine); - var p3 = SubPromise.all([p1, p2]); - result.push(p3.mine); - result.push(p3 instanceof Promise); - result.push(p3 instanceof SubPromise); - - return p3.then(sleep(50), function (it) { - return result.push(it); - }).then(function () { - return console.log(result); - }); + fn(1) + fn(2) + fn(3) diff --git a/test/utils.js b/test/utils.js index c403344..a7bed0d 100644 --- a/test/utils.js +++ b/test/utils.js @@ -465,6 +465,13 @@ module.exports = testSuit("basic", function (it) { }); }); + it("retry multiple call", [1, 2, 3, 4, 5], function () { + var fn = utils.retry(2, 10, function (v) { + return v; + }); + return Yaku.all([fn(1), fn(2), fn(3), fn(4), fn(5)]); + }); + it("guard basic", "err", function () { return Yaku.reject(new TypeError("err")) .then(function () {