Created
September 2, 2017 19:59
-
-
Save matinkaboli/5eeb5a4bfb11ffa2ac0ad355dbb2581e to your computer and use it in GitHub Desktop.
use Generator and Promise to impelement async functions
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* getResponse() { | |
yield new Promise(resolve => { | |
request('example.com', res => { | |
resolve(); | |
}); | |
}); | |
// you can have more functions. It's just a example. just do it | |
// yield new Promise .... | |
} | |
const iterator = getResponse(); | |
(function loop() { | |
const next = iterator.next(); | |
if (next.done) { | |
console.log('All functions is done'); | |
return; | |
} | |
next.value.then(loop); | |
}()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment