Skip to content

Instantly share code, notes, and snippets.

@dtothefp
Created December 19, 2015 14:25
Show Gist options
  • Save dtothefp/ecbaf92a722797bb7f4f to your computer and use it in GitHub Desktop.
Save dtothefp/ecbaf92a722797bb7f4f to your computer and use it in GitHub Desktop.
function spawn(gen) {
const it = gen();
function _co(method, arg) {
let res;
try {
console.log('***TRY***', method, arg);
res = it[method](arg);
} catch(err) {
console.log('***CATCH***', err.message);
return Promise.reject(err);
}
if (res.done) {
if (method === 'throw') {
console.log('***DONE THROW***', arg);
return arg;
} else {
console.log('****DONE VAL***', res.value);
return res.value;
}
} else {
return Promise.resolve(res.value)
.then((val) => {
console.log('***THEN RESOLVE***');
return _co('next', val);
}, (err) => {
console.log('***THEN ERR***', err);
return _co('throw', err);
});
}
}
const ret = _co('next');
console.log('***RET SPAWN PROMISE***', typeof ret.then);
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment