Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add batch callbacks

  • Loading branch information...
commit a324d7537b1de8f1eb2c38f8cecbf4700f5ad4b9 1 parent dc84145
Daniel Baulig authored March 17, 2012

Showing 2 changed files with 35 additions and 6 deletions. Show diff stats Hide diff stats

  1. 26  README.md
  2. 15  lib/first.js
26  README.md
Source Rendered
@@ -29,6 +29,7 @@ Example:
29 29
   * [Sequential & Parallel](#seqandpar)
30 30
   * [Sequence Parameters](#parameters)
31 31
   * [Ordered Parallel Paramters](#parpar)
  32
+  * [Batch Callbacks](#batch)
32 33
   * [Unit Tests](#tests)
33 34
 
34 35
 ## <a name="small" />Small
@@ -90,6 +91,31 @@ Example:
90 91
       // function2[0] === 'Hello, World!'
91 92
     });
92 93
 
  94
+## <a name="batch" />Batch Callbacks
  95
+
  96
+    first(function () {
  97
+      // first do something
  98
+      this();
  99
+    }).
  100
+    then(function () {
  101
+      // then do something else
  102
+      this();
  103
+    }, function {
  104
+      // then do something else
  105
+      this();
  106
+    }).
  107
+    then(function () {
  108
+      // then do something else
  109
+      this();
  110
+    }).
  111
+    whilst(function () {
  112
+      // whilst doing something else
  113
+      this();
  114
+    }, function () {
  115
+      // whilst doing something else
  116
+      this();
  117
+    });
  118
+
93 119
 ## <a name="tests" />Unit Tests
94 120
 
95 121
     make test
15  lib/first.js
... ...
@@ -1,6 +1,9 @@
1 1
 /*jshint eqeqeq:true, proto:true, laxcomma:true, undef:true, node:true, expr: true*/
2 2
 
3  
-var slice = Array.prototype.slice, isArray = require('util').isArray;
  3
+var slice = Array.prototype.slice
  4
+  , isArray = require('util').isArray
  5
+  , push = Array.prototype.push
  6
+  , concat = Array.prototype.concat;
4 7
 
5 8
 module.exports = function first (f) {
6 9
   var functions = [f], next = function () {
@@ -43,16 +46,16 @@ module.exports = function first (f) {
43 46
   };
44 47
 
45 48
   var deferred = {
46  
-    then: function(f) {
47  
-      functions.push(f);
  49
+    then: function() {
  50
+      push.apply(functions, arguments);
48 51
       return this;
49 52
     },
50  
-    whilst: function(f) {
  53
+    whilst: function() {
51 54
       var last = functions.length-1;
52 55
       if (isArray (functions[last])) {
53  
-        functions[last].push(f);
  56
+        push.apply(functions[last], arguments);
54 57
       } else {
55  
-        functions[last] = [functions[last], f];
  58
+        functions[last] = concat.apply([functions[last]], arguments);
56 59
       }
57 60
       return this;
58 61
     }

0 notes on commit a324d75

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