Last active
August 13, 2017 08:21
-
-
Save 6pm/07ec43f73feb930d99407f213dbcc011 to your computer and use it in GitHub Desktop.
iterate urls with fetch and generators
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 executeGenerator(generator, yieldValue) { | |
let next = generator.next(yieldValue); | |
if (!next.done) { | |
next.value.then( | |
result => executeGenerator(generator, result), | |
err => generator.throw(err) | |
); | |
} else { | |
// catch return of generator | |
console.log(next.value); | |
} | |
} | |
function* testGen() { | |
const url = 'https://yesno.wtf/api' | |
let res1 = yield fetch(url).catch(err => console.log('fetch catch err ' + err)) | |
let res2 = yield fetch(`${url}?param1=${res1.ok}`).catch(err => console.log('fetch catch err ' + err)) | |
let res3 = yield fetch(`${url}?param1=${res1.ok}¶m2=${res2.ok}`).catch(err => console.log('fetch catch err ' + err)) | |
return 'end' | |
} | |
executeGenerator(testGen()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment