r/javascript • • Jul 10 '16

The JavaScript language reboot is done

https://medium.com/@velmu/the-javascript-language-reboot-is-done-7bcec4be2cb6#.dy2fyy9j1
41 Upvotes

98 comments sorted by

View all comments

Show parent comments

4

u/konbit Jul 10 '16 edited Jul 10 '16

Here's my favorite:

async function getPosts() {
    const res = await fetch('https://jsonplaceholder.typicode.com/posts');
    const json = await res.json();
    const posts = json.filter(obj => obj.userId === 1);
    console.log(`You have made ${posts.length} posts.`);
}

EDIT: res.json() is a promise

1

u/[deleted] Jul 10 '16

res.json() is also a promise, no?

1

u/konbit Jul 10 '16

Yup, fixed. Still an easier read.

1

u/[deleted] Jul 10 '16

Maybe const json = await fetch('https://jsonplaceholder.typicode.com/posts').then(res => res.json());?