Permalink
Browse files

Stop calling FunctionCall#start on process.nextTick.

Let the client initiate the function call to avoid postponing the call until
process.nextTick.
  • Loading branch information...
1 parent be3e17e commit 1973c1c291f18eab604c3d7e2ead74e20218b3c6 @MathieuTurcotte committed Jan 26, 2013
Showing with 18 additions and 20 deletions.
  1. +8 −7 README.md
  2. +1 −0 examples/function_call.js
  3. +9 −13 index.js
View
@@ -99,6 +99,7 @@ var call = backoff.call(get, 'https://duplika.ca/', function(err, res) {
call.setStrategy(new backoff.ExponentialStrategy());
call.failAfter(10);
+call.start();
```
## API
@@ -125,17 +126,17 @@ and 1. By default, no randomisation is applied on the backoff delay.
### backoff.call(fn, [args, ...], callback)
-- fn: function to call in a backoff handler
+- fn: function to call in a backoff handler, i.e. the wrapped function
- args: function's arguments
- callback: function's callback accepting an error as its first argument
-Calls an asynchronous function in a backoff handler so that it gets
-automatically retried on error. The wrapped function will get retried until it
-succeds or reaches the maximum number of backoffs. In both cases, the callback
-function will be invoked with the last result returned by the wrapped function.
+Constructs a `FunctionCall` instance for the given function. The wrapped
+function will get retried until it succeds or reaches the maximum number
+of backoffs. In both cases, the callback function will be invoked with the
+last result returned by the wrapped function.
-This function returns a `FunctionCall` instance that is going to be invoked on
-next tick and can be used to configure and/or abort the call.
+It is the caller's responsability to initiate the call by invoking the
+`start` method on the returned `FunctionCall` instance.
### Class Backoff
@@ -53,3 +53,4 @@ call.on('backoff', function(number, delay) {
call.setStrategy(new backoff.ExponentialStrategy());
call.failAfter(2);
+call.start();
View
@@ -34,20 +34,16 @@ module.exports.exponential = function(options) {
};
/**
- * Calls a function in a backoff loop.
- * @param fn Function to wrap in a backoff handler.
- * @param vargs Function's arguments (var args).
- * @param callback Function's callback.
- * @return The call handle.
+ * Constructs a FunctionCall for the given function and arguments.
+ * @param fn The function to wrap in a backoff handler.
+ * @param vargs The function's arguments (var args).
+ * @param callback The function's callback.
+ * @return The FunctionCall instance.
*/
module.exports.call = function(fn, vargs, callback) {
var args = Array.prototype.slice.call(arguments);
- var call = new FunctionCall(args[0], args.slice(1, args.length - 1),
- args[args.length - 1]);
-
- process.nextTick(function() {
- call.start();
- });
-
- return call;
+ fn = args[0];
+ vargs = args.slice(1, args.length - 1);
+ callback = args[args.length - 1];
+ return new FunctionCall(fn, vargs, callback);
};

0 comments on commit 1973c1c

Please sign in to comment.