-
-
Save heapwolf/1918046 to your computer and use it in GitHub Desktop.
z
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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; |
z(function(next) { console.log('A'); next(); })
>> z(function(next) { console.log('B'); next(); })
>> z.done(function(err) { console.log('DONE-B'); })
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since lots of people are asking, I'd like to iterate; don't use this for anything. I just wrote this to explain to someone how
valueOf
works.