From 6345ddc4f5b6191e53a580c60389d7899644d13e Mon Sep 17 00:00:00 2001 From: Mike McNeil Date: Tue, 22 May 2018 20:55:51 -0500 Subject: [PATCH] rearrange for clarity, and update comments --- lib/private/Deferred.js | 70 +++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/lib/private/Deferred.js b/lib/private/Deferred.js index 821aa87..76f00d4 100644 --- a/lib/private/Deferred.js +++ b/lib/private/Deferred.js @@ -534,6 +534,40 @@ Deferred.prototype.tolerate = function(negotiationRuleOrWildcardHandler, specifi }; + +/** + * .retry() + * + * Attach an exponential backoff and retry strategy for this invocation. + * + * > See `bindUserlandAfterExecLC` utility for details on how this works. + * + * > WARNING: Please be sure that the function being wrapped is idempotent, or + * > at least that you very, very clearly understand what you're doing before + * > using this function! + * + * @throws {Error} If already begun executing + */ +Deferred.prototype.retry = function (negotiationRuleOrWildcardHandler){ + + if (this._hasBegunExecuting) { + throw flaverr({ + name: + 'UsageError', + message: + 'Could not attach exponential backoff & retry strategy with `.retry()` because\n'+ + 'this invocation has already '+(this._hasTimedOut?'timed out':this._hasFinishedExecuting?'finished executing':'begun executing')+'.' + }, this._omen); + } + + this._errorsThatCausedRetries = []; + this._retryDelaySeries = [100, 200, 400]; + + bindUserlandAfterExecLC('retry', negotiationRuleOrWildcardHandler, undefined, this); + return this; +}; + + /** * .toPromise() * @@ -717,39 +751,6 @@ Deferred.prototype.timeout = function (ms){ }; -/** - * .retry() - * - * Attach an exponential backoff and retry strategy for this invocation. - * - * > See `bindUserlandAfterExecLC` utility for details on how this works. - * - * > WARNING: Please be sure that the function being wrapped is idempotent, or - * > at least that you very, very clearly understand what you're doing before - * > using this function! - * - * @throws {Error} If already begun executing - */ -Deferred.prototype.retry = function (negotiationRuleOrWildcardHandler){ - - if (this._hasBegunExecuting) { - throw flaverr({ - name: - 'UsageError', - message: - 'Could not attach exponential backoff & retry strategy with `.retry()` because\n'+ - 'this invocation has already '+(this._hasTimedOut?'timed out':this._hasFinishedExecuting?'finished executing':'begun executing')+'.' - }, this._omen); - } - - this._errorsThatCausedRetries = []; - this._retryDelaySeries = [100, 200, 400]; - - bindUserlandAfterExecLC('retry', negotiationRuleOrWildcardHandler, undefined, this); - return this; -}; - - // Attach `inspect`, `toString`, and `toJSON` functions // (This is mainly to hide the `_omen` property, which is pretty scary-looking) Deferred.prototype.toJSON = function (){ return null; }; @@ -1262,7 +1263,8 @@ function proceedToFinalAfterExecLC(errCbArg, resultCbArg, extraCbArgs, self, _cb /** * bindUserlandAfterExecLC() * - * Used exclusively by `Deferred.prototype.intercept()` & `.tolerate()`, this function is an optimization. + * Used exclusively by `Deferred.prototype.intercept()`, `Deferred.prototype.tolerate()`, + * and `Deferred.prototype.retry()`, this function is an optimization. * It would be much better to use an IIFE instead of defining this function, but we're * dealing with a very hot code path, so the performance gain is worth it. * That said, this optimization should never be applied in normal userland code!