Permalink
Browse files

Add batch callbacks

  • Loading branch information...
1 parent dc84145 commit a324d7537b1de8f1eb2c38f8cecbf4700f5ad4b9 @DanielBaulig committed Mar 17, 2012
Showing with 35 additions and 6 deletions.
  1. +26 −0 README.md
  2. +9 −6 lib/first.js
View
@@ -29,6 +29,7 @@ Example:
* [Sequential & Parallel](#seqandpar)
* [Sequence Parameters](#parameters)
* [Ordered Parallel Paramters](#parpar)
+ * [Batch Callbacks](#batch)
* [Unit Tests](#tests)
## <a name="small" />Small
@@ -90,6 +91,31 @@ Example:
// function2[0] === 'Hello, World!'
});
+## <a name="batch" />Batch Callbacks
+
+ first(function () {
+ // first do something
+ this();
+ }).
+ then(function () {
+ // then do something else
+ this();
+ }, function {
+ // then do something else
+ this();
+ }).
+ then(function () {
+ // then do something else
+ this();
+ }).
+ whilst(function () {
+ // whilst doing something else
+ this();
+ }, function () {
+ // whilst doing something else
+ this();
+ });
+
## <a name="tests" />Unit Tests
make test
View
@@ -1,6 +1,9 @@
/*jshint eqeqeq:true, proto:true, laxcomma:true, undef:true, node:true, expr: true*/
-var slice = Array.prototype.slice, isArray = require('util').isArray;
+var slice = Array.prototype.slice
+ , isArray = require('util').isArray
+ , push = Array.prototype.push
+ , concat = Array.prototype.concat;
module.exports = function first (f) {
var functions = [f], next = function () {
@@ -43,16 +46,16 @@ module.exports = function first (f) {
};
var deferred = {
- then: function(f) {
- functions.push(f);
+ then: function() {
+ push.apply(functions, arguments);
return this;
},
- whilst: function(f) {
+ whilst: function() {
var last = functions.length-1;
if (isArray (functions[last])) {
- functions[last].push(f);
+ push.apply(functions[last], arguments);
} else {
- functions[last] = [functions[last], f];
+ functions[last] = concat.apply([functions[last]], arguments);
}
return this;
}

0 comments on commit a324d75

Please sign in to comment.