Created
February 21, 2016 16:54
-
-
Save Qaaj/b78adb528c171cb1aeac to your computer and use it in GitHub Desktop.
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
// 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