Last active
September 12, 2015 22:11
-
-
Save grgur/ab39f4007d5ce1174c81 to your computer and use it in GitHub Desktop.
Demo of async/await with ES6 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 makeRequest(url) { | |
fetch(url) | |
.then(response => response.json()) | |
.then(json => it.next(json)) | |
.catch(error => console.error('Somthing shit the bed', error)); | |
} | |
function *syncRequests() { | |
const redditUrl = 'https://www.reddit.com/controversial.json?count=1&limit=2'; | |
const page1 = yield makeRequest(redditUrl); | |
const { after } = page1.data; | |
const page2 = yield makeRequest(`${redditUrl}&after=${after}`); | |
console.log('Page 2', page2); | |
} | |
var it = syncRequests(); | |
it.next(); | |
// Page 2 Object {kind: "Listing", data: Object} |
page1.data.after
is less characters and more readable than const { after } = page1.data;
:P
@Daniel15 this is all quite new and it's hard to say which convention is better. I prefer sticking to a way of doing particular things persistently vs counting characters.
E.g.const { a, b, c, d } from someObj
will certainly take less chars than going the 'old' way about it.
I would also enjoy if someone questioned what those curly braces mean and went ahead to learn more. That's what this is all about isn't it ;)
+1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An easy way to run this is if you have Scratch JS installed in your Chrome.