Created
February 4, 2017 22:09
-
-
Save tweinfeld/c3940e5f60931152b4dacbe7765274fa to your computer and use it in GitHub Desktop.
Generator Promise Yielder
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
const run = function(gen, ...args){ | |
let iterator = gen.apply(undefined, args); | |
return new Promise((resolve)=>{ | |
const rep = function(previousValue){ | |
let { done, value } = iterator[previousValue instanceof Error ? "throw" : "next"](previousValue); | |
!done ? ((value instanceof Promise ? value.then((v)=>rep(v)).catch((v)=>rep(new Error(v))) : rep(value))) : resolve(value); | |
} | |
rep(); | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment