Skip to content

Instantly share code, notes, and snippets.

@cwg999
Created January 29, 2018 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwg999/552cab0140b8770e2d6a79eadd3eadb0 to your computer and use it in GitHub Desktop.
Save cwg999/552cab0140b8770e2d6a79eadd3eadb0 to your computer and use it in GitHub Desktop.
Generator Promise For Loop example
(function(){ // Generator Promise For Loop
let getFiringResult = delay => new Promise((o,x)=>setTimeout(()=>o(Math.random()),delay));
const fireCannons = (function* (data){
let index = 0;
while(index<5){
index++;
yield getFiringResult(1000);
}
return;
})();
const reload = data => {
console.log(data);
let {value:next,done} = fireCannons.next()
if(!done) next.then(reload).catch(err=>console.log(err));
};
fireCannons.next().value.then(reload).catch(err=>console.log(err));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment