diff --git a/README.md b/README.md index 41a5cef..252f711 100644 --- a/README.md +++ b/README.md @@ -1724,14 +1724,15 @@ Retry async operation **Kind**: static method of [CPromise](#module_CPromise..CPromise) -| Param | Type | -| --- | --- | -| fn | CPGeneratorRetryFunction \| CPRetryFunction | -| [options] | object | -| [options.args] | array | -| [options.retries] | number | -| [options.delayWeight] | number \| CPRetryDelayResolver | -| [options.delay] | object | +| Param | Type | Default | +| --- | --- | --- | +| fn | CPGeneratorRetryFunction \| CPRetryFunction | | +| [options] | Object | | +| [options.args] | Array | | +| [options.retries] | Number | | +| [options.delayWeight] | Number | | +| [options.delay] | Number \| CPRetryDelayResolver | | +| [options.scopeArg] | Boolean | false | diff --git a/lib/c-promise.js b/lib/c-promise.js index 0ddf7a3..9c35724 100644 --- a/lib/c-promise.js +++ b/lib/c-promise.js @@ -1526,11 +1526,12 @@ const CPromise= class CPromise extends Promise { /** * Retry async operation * @param {CPGeneratorRetryFunction|CPRetryFunction} fn - * @param {object} [options] - * @param {array} [options.args] - * @param {number} [options.retries] - * @param {number|CPRetryDelayResolver} [options.delayWeight] - * @param {object} [options.delay] + * @param {Object} [options] + * @param {Array} [options.args] + * @param {Number} [options.retries] + * @param {Number} [options.delayWeight] + * @param {Number|CPRetryDelayResolver} [options.delay] + * @param {Boolean} [options.scopeArg= false] * @return {CPromise} */ static retry(fn, options) { @@ -1538,18 +1539,20 @@ const CPromise= class CPromise extends Promise { throw TypeError('fn must be a function'); } - options!==undefined && validateOptions(options, { + options !== undefined && validateOptions(options, { args: array(), retries: numberFinitePositive, delayWeight: numberFinitePositive, - delay: union(numberFinitePositive, functionPlain) + delay: union(numberFinitePositive, functionPlain), + scopeArg: boolean }); const { args = [], retries = 3, delayWeight= 0.1, - delay = (attempt) => attempt * 1000 + delay = (attempt) => attempt * 1000, + scopeArg= false } = typeof options === 'number' ? {retries: options} : options || {}; let attemptIndex = 0; @@ -1562,7 +1565,9 @@ const CPromise= class CPromise extends Promise { const doAttempt = () => { return isGeneratorFunction(fn) ? - this.run(fn, {args: fnArgs, scopeArg: true}) : this.resolve(fn.apply(scope, fnArgs)); + this.run(fn, {args: fnArgs, scopeArg}) : new this((resolve, reject, _scope) => { + resolve(fn.apply(_scope, scopeArg ? [_scope, attemptIndex, args] : fnArgs)); + }) } let _delay = delay;