Created
January 29, 2018 19:57
-
-
Save cwg999/552cab0140b8770e2d6a79eadd3eadb0 to your computer and use it in GitHub Desktop.
Generator Promise For Loop example
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(){ // 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