Skip to content

Instantly share code, notes, and snippets.

@amovah
Last active September 2, 2017 19:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amovah/52c2826c638fe35ee3f7 to your computer and use it in GitHub Desktop.
Save amovah/52c2826c638fe35ee3f7 to your computer and use it in GitHub Desktop.
use Generator and Promise to impelement async functions
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