Last active
October 21, 2016 07:06
-
-
Save gurdasnijor/f0a0180edcec41257f55ff952106b6e6 to your computer and use it in GitHub Desktop.
co in 16 lines
This file contains 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 co(genFunc) { | |
let genObj = genFunc(); | |
run(); | |
function run(promiseResult = undefined) { | |
let item = genObj.next(promiseResult); | |
if (!item.done) { | |
// A promise was yielded | |
item.value | |
.then(result => run(result)) | |
.catch(error => { | |
genObj.throw(error); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment