Skip to content

Instantly share code, notes, and snippets.

@tweinfeld
Created February 4, 2017 22:09
Show Gist options
  • Save tweinfeld/c3940e5f60931152b4dacbe7765274fa to your computer and use it in GitHub Desktop.
Save tweinfeld/c3940e5f60931152b4dacbe7765274fa to your computer and use it in GitHub Desktop.
Generator Promise Yielder
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