Skip to content

Instantly share code, notes, and snippets.

@grgur
Last active September 12, 2015 22:11
Show Gist options
  • Save grgur/ab39f4007d5ce1174c81 to your computer and use it in GitHub Desktop.
Save grgur/ab39f4007d5ce1174c81 to your computer and use it in GitHub Desktop.
Demo of async/await with ES6 generators
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}
@Daniel15
Copy link

Daniel15 commented Sep 9, 2015

page1.data.after is less characters and more readable than const { after } = page1.data; :P

@grgur
Copy link
Author

grgur commented Sep 12, 2015

@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