Skip to content
Browse files

Initial commit

  • Loading branch information...
0 parents commit df3520fe8b15b47c9566d413bc7cdcaa0692188b @creationix committed Apr 6, 2010
Showing with 61 additions and 0 deletions.
  1. 0 README.markdown
  2. +61 −0 lib/step.js
0 README.markdown
No changes.
61 lib/step.js
@@ -0,0 +1,61 @@
+/*
+Copyright (c) 2010 Tim Caswell <tim@creationix.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+// Inspired by http://github.com/willconant/flow-js, but reimplemented and
+// modified to fit my taste and the node.JS error handling system.
+function Step() {
+ var steps = Array.prototype.slice.call(arguments),
+ counter, results;
+ function next() {
+ if (steps.length === 0) {
+ return;
+ }
+ var fn = steps.shift();
+ counter = 0;
+ results = [];
+ try {
+ fn.apply(next, arguments);
+ } catch (e) {
+ next(e);
+ }
+ }
+ next.parallel = function () {
+ var i = counter;
+ counter++;
+ return function () {
@xavi-
xavi- added a note Feb 6, 2011

Why not add a err and result parameters to this function? That way you don't have use arguments[0] and arguments[1] further down in code. I feel it makes the purpose of this function much more clear as well as improve general code readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ counter--;
+ if (arguments[0]) {
+ results[0] = arguments[0];
+ }
+ results[i + 1] = arguments[1];
+ if (counter <= 0) {
+ next.apply(null, results);
+ }
+ };
+ };
+ next([]);
+}
+
+// Hook into commonJS module systems
+if (typeof module !== 'undefined' && "exports" in module) {
+ module.exports = Step;
+}

0 comments on commit df3520f

Please sign in to comment.
Something went wrong with that request. Please try again.