Skip to content

Instantly share code, notes, and snippets.

@ericchaves
Created December 10, 2017 21:32
Show Gist options
  • Select an option

  • Save ericchaves/cc33acc0ccb29b1cfc44350e5b39fc16 to your computer and use it in GitHub Desktop.

Select an option

Save ericchaves/cc33acc0ccb29b1cfc44350e5b39fc16 to your computer and use it in GitHub Desktop.
Poor's man coroutine
/*
* Poor's man coroutine
*/
function co(g) {
return new Promise((resolve, reject)=>{
let it = g(), ret;
(function iterate(val){
ret = it.next(val)
if(ret.done) return resolve(val)
else if (ret.value instanceof Promise) ret.value.then(iterate).catch(reject)
else process.nextTick(()=>iterate(ret.value))
})()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment