Skip to content

Instantly share code, notes, and snippets.

@iannbing
Created March 8, 2018 22:16
Show Gist options
  • Save iannbing/d4e2c65f21dfc3158a903e200184f79a to your computer and use it in GitHub Desktop.
Save iannbing/d4e2c65f21dfc3158a903e200184f79a to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/geqazuv
<script src="http://static.jsbin.com/js/vendor/traceur.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
function* createQuoteFetcher() {
const response = yield fetch('http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=json')
const quote = yield response.json()
return `${quote.quoteText} —${quote.quoteAuthor}`
}
const coroutine = (gen) => {
const generator = gen()
const handle = (result) => {
if (result.done) return Promise.resolve(result.value)
return Promise.resolve(result.value)
.then(res => handle(generator.next(res)))
}
return handle(generator.next())
}
const quoteFetcher = coroutine(createQuoteFetcher)
quoteFetcher.then(quote => console.log(quote))
</script>
<script id="jsbin-source-javascript" type="text/javascript">function* createQuoteFetcher() {
const response = yield fetch('http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=json')
const quote = yield response.json()
return `${quote.quoteText} —${quote.quoteAuthor}`
}
const coroutine = (gen) => {
const generator = gen()
const handle = (result) => {
if (result.done) return Promise.resolve(result.value)
return Promise.resolve(result.value)
.then(res => handle(generator.next(res)))
}
return handle(generator.next())
}
const quoteFetcher = coroutine(createQuoteFetcher)
quoteFetcher.then(quote => console.log(quote))
</script></body>
</html>
function* createQuoteFetcher() {
const response = yield fetch('http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=json')
const quote = yield response.json()
return `${quote.quoteText} —${quote.quoteAuthor}`
}
const coroutine = (gen) => {
const generator = gen()
const handle = (result) => {
if (result.done) return Promise.resolve(result.value)
return Promise.resolve(result.value)
.then(res => handle(generator.next(res)))
}
return handle(generator.next())
}
const quoteFetcher = coroutine(createQuoteFetcher)
quoteFetcher.then(quote => console.log(quote))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment