Permalink
Browse files

Add wait

  • Loading branch information...
1 parent 04fbaa7 commit a609593f8db154b8bb494e76f81c95dfc5a26594 @hokaccha committed Nov 9, 2011
Showing with 60 additions and 1 deletion.
  1. +34 −1 lib/chain-tiny.js
  2. +26 −0 test/basic.js
View
@@ -156,9 +156,30 @@ Chain.prototype.forEachParallel = function(fn) {
});
};
+
+/**
+ * Wait next process during the `time` (ms).
+ *
+ * @param {time} Int
+ * @return {Object} chain
+ * @api public
+ */
+Chain.prototype.wait = function(time) {
+ var self = this;
+
+ return self.chain(function() {
+ var args = Array.prototype.slice.call(arguments);
+ var next = args.pop();
+ args.unshift(null);
+ setTimeout(function() {
+ next.apply(null, args);
+ }, time);
+ });
+};
+
+// todo doc
Chain.parallel = function(ary) {
return Chain().parallel(ary);
-
};
/**
@@ -189,4 +210,16 @@ Chain.forEachParallel = function(ary, fn) {
}).forEachParallel(fn);
};
+/**
+ * Static method for `.wait()`.
+ *
+ * @param {time} Int
+ * @return {Object} chain
+ * @api public
+ */
+Chain.wait = function(time) {
+ return Chain(function(next) { next(null); }).wait(time);
+};
+
+
module.exports = Chain;
View
@@ -166,5 +166,31 @@ module.exports = nodeunit.testCase({
t.deepEqual(result, ['0foo', '1bar']);
t.done();
});
+ },
+ wait: function(t) {
+ chain(function(next) {
+ next(null, 'foo', 'bar');
+ })
+ .wait(100)
+ .chain(function(foo, bar, next) {
+ t.equal(foo, 'foo');
+ t.equal(bar, 'bar');
+ next();
+ })
+ .end(function(err) {
+ t.equal(err, null);
+ t.done();
+ });
+ },
+ 'Chain.wait': function(t) {
+ chain
+ .wait(100)
+ .chain(function(next) {
+ next();
+ })
+ .end(function(err) {
+ t.equal(err, null);
+ t.done();
+ });
}
});

0 comments on commit a609593

Please sign in to comment.