fcoin/lib/utils/spawn.js
Christopher Jeffrey d78151d3d3
refactor: promises.
2016-09-20 15:28:14 -07:00

45 lines
898 B
JavaScript

'use strict';
// See: https://github.com/yoursnetwork/asink
function spawn(genF, self) {
return new Promise(function(resolve, reject) {
var gen = genF.call(self);
function step(nextF) {
var next;
try {
next = nextF();
} catch (e) {
// finished with failure, reject the promise
reject(e);
return;
}
if (next.done) {
// finished with success, resolve the promise
resolve(next.value);
return;
}
// not finished, chain off the yielded promise and `step` again
Promise.resolve(next.value).then(function(v) {
step(function() {
return gen.next(v);
});
}, function (e) {
step(function() {
return gen.throw(e);
});
});
}
step(function() {
return gen.next(undefined);
});
});
}
module.exports = spawn;