Skip to content

Instantly share code, notes, and snippets.

@kriskowal
Created August 13, 2014 18:05
Show Gist options
  • Save kriskowal/5bf27a3c734951d2b838 to your computer and use it in GitHub Desktop.
Save kriskowal/5bf27a3c734951d2b838 to your computer and use it in GitHub Desktop.
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