Created
December 10, 2017 21:32
-
-
Save ericchaves/cc33acc0ccb29b1cfc44350e5b39fc16 to your computer and use it in GitHub Desktop.
Poor's man coroutine
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
| /* | |
| * 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