Skip to content

Commit

Permalink
rearrange for clarity, and update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mikermcneil committed May 23, 2018
1 parent ba24984 commit 6345ddc
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions lib/private/Deferred.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
*
Expand Down Expand Up @@ -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; };
Expand Down Expand Up @@ -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!
Expand Down

0 comments on commit 6345ddc

Please sign in to comment.