Permalink
Browse files

Added Batch helper

  • Loading branch information...
tj committed Dec 18, 2011
1 parent 119d995 commit 82b2a0d6cefd81763510d180b7605e64f98744de
Showing with 58 additions and 0 deletions.
  1. +58 −0 lib/batch.js
View
@@ -0,0 +1,58 @@
+
+/**
+ * Expose `Batch`.
+ */
+
+module.exports = Batch;
+
+/**
+ * Create a new Batch.
+ */
+
+function Batch() {
+ this.fns = [];
+ for (var i = 0, len = arguments.length; i < len; ++i) {
+ this.push(arguments[i]);
+ }
+}
+
+/**
+ * Queue a function.
+ *
+ * @param {Function} fn
+ * @return {Batch}
+ * @api public
+ */
+
+Batch.prototype.push = function(fn){
+ this.fns.push(fn);
+ return this;
+};
+
+/**
+ * Execute all queued functions in parallel,
+ * executing `cb(err, results)`.
+ *
+ * @param {Function} cb
+ * @return {Batch}
+ * @api public
+ */
+
+Batch.prototype.end = function(cb){
+ var pending = this.fns.length
+ , results = []
+ , done;
+
+ if (!this.fns.length) return cb(null, results);
+
+ this.fns.forEach(function(fn){
+ fn(function(err, res){
+ if (done) return;
+ if (err) return done = true, cb(err);
+ results.push(res);
+ --pending || cb(null, results);
+ });
+ });
+
+ return this;
+};

0 comments on commit 82b2a0d

Please sign in to comment.