Created
March 8, 2018 22:16
-
-
Save iannbing/d4e2c65f21dfc3158a903e200184f79a to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/geqazuv
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
<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> |
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* 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