Skip to content

Instantly share code, notes, and snippets.

@Qaaj
Created February 21, 2016 16:54
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 Qaaj/b78adb528c171cb1aeac to your computer and use it in GitHub Desktop.
Save Qaaj/b78adb528c171cb1aeac to your computer and use it in GitHub Desktop.
// This function will return a promise that will fetch data from a certain URL
function getURLPromise(url) {
return new Promise((resolve, reject) => {
http.get(url, (rs) => {
var data = ''
rs.on('data', (chunk) => data += chunk);
rs.on('end', ()=> resolve(data.length));
});
});
}
// Create our generator
function *myGen() {
var a = yield getURLPromise('http://www.google.com')
var b = yield getURLPromise('http://www.apple.com')
var c = yield getURLPromise('http://www.microsoft.com')
debug([a, b, c]);
}
// Instance of our generator
let gen = myGen();
// Function that will resolve our promise and pass on the result to the generator
function resolvePromise(gen, result) {
gen.next(result).value.then(result => {
resolvePromise(gen, result);
});
}
// Start resolving promises
resolvePromise(gen, null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment