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

3

u/lucidguppy Jul 10 '16

I'm afraid that the cycle will start again - and things will be reinvented.

Has anything really become simpler?

24

u/[deleted] Jul 10 '16

Has anything really become simpler?

Are you kidding me?

fetch('https://jsonplaceholder.typicode.com/posts').then(response => {
	if (response.status === 200) {
		response.json().then(data => {
			const filteredPosts = data.filter(obj => obj.userId === 1);
			console.log(`You have made ${filteredPosts.length} posts.`);
		});
	}
});

vs

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
	if (xhttp.readyState == 4 && xhttp.status == 200) {
		var posts = JSON.parse(xhttp.responseText);
		
		var filteredPosts = [];
		for (var i = 0; i < posts.length; i++) {
			var post = posts[i];
			if (post.userId === 1) {
				filteredPosts.push(post);
			}
		}
		
		console.log('You have made ' + filteredPosts.length + ' posts.');
	}
};
xhttp.open("GET", "http://jsonplaceholder.typicode.com/posts", true);
xhttp.send();

0

u/[deleted] Jul 10 '16

[deleted]

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

I agree, never going back from async/await if I get to choose

1

u/[deleted] Jul 10 '16

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

1

u/TheLameloid Jul 10 '16

Is there a way to extract the result of the promise out of the .then() callback (which I assume is what the await keyboard does) without running into race conditions errors?

1

u/konbit Jul 10 '16

Nope. That's precisely why async await is so awesome.

1

u/[deleted] Jul 10 '16

Cool, I wasn't aware you could chain thens like that. Although this version doesn't check if the status was ok =P

0

u/[deleted] Jul 10 '16

[deleted]

10

u/pertheusual Jul 10 '16

That's not true, the promise returned by fetch() only rejects if there is a proper network error or the connection itself doesn't get established. No matter what the status code is, it'll still resolve. There's nothing wrong with returning JSON in the body of a 500 for instance.

If there was a 404 with no JSON, your example would just throw an exception because json would be undefined so undefined.filter would be non-existant.

8

u/Klathmon Jul 10 '16

And the same thing with async/await is even simpler.

3

u/jcready __proto__ Jul 10 '16

You cannot abort a fetch though.

→ More replies (3)

0

u/[deleted] Jul 10 '16

[deleted]

1

u/[deleted] Jul 10 '16

but now those days are over

They absolutely are not over, nothing has been removed from the language. The only thing making people use all these complicated systems are social pressures. The only person you have to blame for losing simplicity is yourself (or your team, if they're the instigators).

1

u/[deleted] Jul 10 '16

Yeah, despite working with arrow functions for nearly a year, I still find them infinitely more difficult to understand and comprehend than the non-arrow version. The syntax is just so poor.

12

u/hyperhopper Jul 10 '16

Alright, I really hate to be that asshole on the internet, but if you are working with them for a year, and can't figure it out, programming might not be for you.

let that = this;
function (a) {
    return a + that.b;
}

is the same as

a => a + this.b

It's really not difficult or novel at all.

2

u/WorkshopX Jul 10 '16

upvote for a simple example of arrow functions effect on this binding. Well done.

→ More replies (3)
→ More replies (4)