public
Created — forked from hij1nx/z.js

z

  • Download Gist
z.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
//
// 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;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.