Created
August 13, 2014 18:05
-
-
Save kriskowal/5bf27a3c734951d2b838 to your computer and use it in GitHub Desktop.
This file contains 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
PromiseIterator.prototype.fork = function (length) { | |
var buffers = new Array(length).map(function () { | |
return new Buffer(); | |
}); | |
var inputs = buffers.map(function (buffer) { | |
return buffer.in; | |
}); | |
var outputs = buffers.map(function (buffer) { | |
return buffer.out; | |
}); | |
this.forEach(function (value, index) { | |
return Promise.all(inputs.map(function (input) { | |
return input.yield(value, index); | |
})); | |
}).then(function (value) { | |
return Promise.all(inputs.map(function (input) { | |
return input.return(value); | |
})); | |
}, function (error) { | |
return Promise.all(inputs.map(function (input) { | |
return input.throw(value); | |
})); | |
}).done(); | |
return outputs; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment