Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix a bug in Step.fn, now it works right.

  • Loading branch information...
commit 08dfdc3a28d2936f5c3d8ecdbb32c27bd188d7f9 1 parent 4840376
@creationix authored
Showing with 15 additions and 14 deletions.
  1. +15 −14 lib/step.js
View
29 lib/step.js
@@ -25,7 +25,7 @@ SOFTWARE.
function Step() {
var steps = Array.prototype.slice.call(arguments),
counter, results;
-
+
// Define the main callback that's given as `this` to the steps.
function next() {
@@ -37,12 +37,12 @@ function Step() {
}
return;
}
-
+
// Get the next step to execute
var fn = steps.shift();
counter = 0;
results = [];
-
+
// Run the step in a try..catch block so exceptions don't get out of hand.
try {
var result = fn.apply(next, arguments);
@@ -55,9 +55,9 @@ function Step() {
if (result !== undefined) {
next(undefined, result);
}
-
+
}
-
+
// Add a special callback generator `this.parallel()` that groups stuff.
next.parallel = function () {
var i = counter;
@@ -67,21 +67,21 @@ function Step() {
// Compress the error from any result to the first argument
if (arguments[0]) {
results[0] = arguments[0];
- }
+ }
// Send the other results as arguments
results[i + 1] = arguments[1];
- if (counter <= 0) {
+ if (counter === 0) {
// When they're all done, call the callback
next.apply(null, results);
}
};
};
-
+
// Start the engine an pass nothing to the first step.
next([]);
}
-// Tack on leading and tailing steps for input and output and return
+// Tack on leading and tailing steps for input and output and return
// the whole thing as a function. Basically turns step calls into function
// factories.
Step.fn = function StepFn() {
@@ -90,16 +90,17 @@ Step.fn = function StepFn() {
var args = Array.prototype.slice.call(arguments);
// Insert a first step that primes the data stream
- steps.unshift(function () {
+ var toRun = [function () {
this.apply(null, args);
- });
+ }].concat(steps);
// If the last arg is a function add it as a last step
if (typeof args[args.length-1] === 'function') {
- steps.push(args.pop());
+ toRun.push(args.pop());
}
-
- Step.apply(null, steps);
+
+
+ Step.apply(null, toRun);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.