Created
December 19, 2015 14:25
-
-
Save dtothefp/ecbaf92a722797bb7f4f to your computer and use it in GitHub Desktop.
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
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