Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Forked from heapwolf/z.js
Created February 27, 2012 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwaldron/1925495 to your computer and use it in GitHub Desktop.
Save rwaldron/1925495 to your computer and use it in GitHub Desktop.
z
//
// an experimental flow control library.
// please dont ever use this for anything.
//
var z = function z(f) {
if (!(this instanceof z)) {
return new z(f);
}
this.f = f;
};
z.iterator = function(f) { z.itr = f; };
z.array = function(a) { z.arr = a; };
z.done = function(f) { z.dn = f; };
z.done.valueOf = function() {
z.arr.forEach(function(i) {
var done = function() {
z.completed += 1;
if (z.completed === z.arr.length) {
z.dn();
}
}
if (z.itr) {
z.itr(i, done);
}
else {
i(done);
}
});
z.itr = null;
z.completed = 0;
z.arr = [];
};
z.arr = [];
z.completed = 0;
z.prototype.valueOf = function() {
z.arr.push(this.f);
}
//
// call an iterator for every item in the array
// and then when they have all completed call the
// done function.
//
z.array(['foo', 'bar']);
z.iterator(function(val, next) { console.log(val); next(); });
z.done(function(err) { console.log('DONE-A'); });
z.array >> z.done;
//
// call all functions until they are all done, then
// call a final function.
//
var a = z(function(next) { console.log('A'); next(); });
var b = z(function(next) { console.log('B'); next(); });
z.done(function(err) { console.log('DONE-B'); });
a >> b >> z.done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment