Skip to content

Instantly share code, notes, and snippets.

@6pm
Last active August 13, 2017 08:21
Show Gist options
  • Save 6pm/07ec43f73feb930d99407f213dbcc011 to your computer and use it in GitHub Desktop.
Save 6pm/07ec43f73feb930d99407f213dbcc011 to your computer and use it in GitHub Desktop.
iterate urls with fetch and generators
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}&param2=${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