r/javascript • u/velmu3k • Jul 10 '16
The JavaScript language reboot is done
https://medium.com/@velmu/the-javascript-language-reboot-is-done-7bcec4be2cb6#.dy2fyy9j116
23
Jul 10 '16
Link to the original article that this one is reproducing.
0
Jul 10 '16
[deleted]
6
u/BlueOak777 Jul 10 '16
OP's link is the orig authors blog. Looks like he wrote about it at the first link and then copypasted it to his blog. Could be two users reposting stuff...could be alt accounts of the author. Who knows.
1
1
13
u/kenman Jul 10 '16
Hi /u/velmu3k, why did you repost the same article but from a different host?
You've already been warned once about shady practices to avoid spam detection, and now you're continuing to do so in a different manner. Consider this your final warning; any further attempts to avoid spam detection will be dealt with swiftly and firmly.
6
u/akujinhikari Jul 10 '16
Not to sound like a dick, but I stopped reading at "catched on."
2
u/homesweetocean Jul 11 '16
I kept reading, but only because I wasn't done with my coffee yet and wanted a few more chuckles.
29
Jul 10 '16
Callbacks obsolete? Seems premature to say so.
10
u/ihsw Jul 10 '16
I think it is.
Callback hell is over, promises and async/await have won.
I challenge you to find any new ES6/TypeScript projects that still use anonymous functions for asynchronous execution.
11
u/gurenkagurenda Jul 10 '16
There are cases where callbacks still make sense though, like event handling.
6
u/Sinistralis Jul 11 '16
Sounds like we need a library to fix this travesty
1
u/gurenkagurenda Jul 11 '16
I think you're joking, but honestly I really do think event handling could use a better abstraction, the same way promises did for procedural async. I don't really know what it would look like though. Something something streams, something something generators.
10
u/namesandfaces Jul 10 '16
I actually find callbacks very manageable with observable streams, just as easy as promises. Code ends up just as comprehensible.
3
Jul 10 '16
I agree. I've never found major issue with callbacks.
IMO, so many of the "problems" with JS are due to the vast majority of people who look at the community and apply fixes/patterns/designs without understand how it actually affects their code.
0
u/TheIncredibleWalrus Jul 10 '16
Callbacks aren't bad because of "callback hell" and the "pyramid of hell", it's about inversion of control and the trust problem
0
u/cosinezero Jul 11 '16
Don’t call my callback too early Don’t call my callback too late Don’t call my callback too few times Don’t call my callback too many times Make sure to provide my callback with any necessary state/parameters Make sure to notify me if my callback fails in some wayOh my god, these apply to every function call in my application! Promises are the hammers to all the nails!
0
Jul 10 '16
[deleted]
1
u/TheIncredibleWalrus Jul 10 '16
Amen, I can't believe the amount of replies it took for this reply to come up (out of a few agreeing upon the OP's misguided view on callbacks).
7
u/hahaNodeJS Jul 10 '16 edited Jul 10 '16
I challenge you to find any new ES6/TypeScript projects that still use anonymous functions for asynchronous execution.
...
new Promise(function(resolve, reject) { resolve(); }) .then(function() { console.log('hello'); });There are two anonymous functions and two callbacks in use here.
0
Jul 10 '16
[deleted]
14
u/Jafit Jul 10 '16
What's promise hell? Is that where you try to use promises without knowing how they work?
3
u/bighi Jul 10 '16
It's when you make promises you can't fulfill and your looks at you with that look on her face.
1
0
Jul 11 '16
[deleted]
1
u/Jafit Jul 11 '16
Can you provide an example of good code layout making callbacks not horrible to use?
0
Jul 11 '16
[deleted]
0
u/Jafit Jul 11 '16
Well most of my day involves writing Javascript for node.js, so there'll be a lot of situations where an operation requires multiple asynchronous database calls, or to verify something in redis before continuing, and things down the line will depend on having access to the results of previous async operations.
If this were to be done using callbacks it would involve a lot of unavoidable nesting.
0
Jul 11 '16
[deleted]
1
u/Jafit Jul 11 '16
I'm not saying that callbacks are best in every situation, but I am saying that promises also aren't best in every situation.
I disagree, I find that promises are a very useful pattern for most situations. With Javascript being a single-threaded, async operations are a fundamental part of writing good Javascript, because especially with Node you really can't afford to have synchronous code blocking the stack, because it'll block the stack for literally every user connected to that specific instance of your application.
A lot of our code at work is architected around promises as a standard interface. If we use a library or module that uses callbacks, we'll use es6-promisify to wrap that in a promise so that its easier to work with.
The only instance I can think of where plain callbacks make sense are in event handlers, such as in node.js streams or event listeners in the browser.
1
u/Martin_Ehrental Jul 10 '16 edited Jul 12 '16
I think the part about async work is premature.
It's true for callbacks; I think Promises have replaced them and soon we will have async/await which should ease working with them... So, almost there.
Concerning events, Observables are replacing event listeners. Most frameworks have settles on an observable library, and there will be soon be a standard Observable set of interfaces... hopefully. But we are not there yet and it's still very unstable. We even get new observable libraries popping up.
5
2
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
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();8
3
-6
u/constructivCritic Jul 10 '16 edited Jul 11 '16
So maybe just because I haven't learned about new method of doing things, but the xmlhttp version seems clearer to me.
EDIT: Whoever downvoted my comment...you're a dumbass for using the downvote button incorrectly. I was provoking some thought or some replies to better understand why the new way was better, I wasn't asking you to agree or disagree with me. Hiding my comment with downvotes detracts from the discussion... That's the opposite of what you want on a site like Reddit.
2
u/fzammetti Jul 10 '16
I for one agree with you. I think people are mistaking shortness and terseness for intrinsically better. Sometimes, more code is more expressive and the examples given here versus the older example, to my mind, bear that out.
0
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
Jul 10 '16
res.json() is also a promise, no?
1
u/konbit Jul 10 '16
Yup, fixed. Still an easier read.
1
1
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
1
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 =P0
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
jsonwould beundefinedsoundefined.filterwould be non-existant.0
Jul 10 '16
[deleted]
1
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
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.bIt'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.
1
Jul 10 '16 edited Jul 10 '16
I've been a programmer for 20 years. I have no problem figuring them out. It's just the syntax I have a problem with. And by problem I don't mean I don't understand it, I mean the syntax was poorly thought out and all around sucks. And yes, I know that the syntax was borrowed from other languages, but that doesn't mean the syntax didn't suck in those other languages too.
1
u/SuperFLEB Jul 10 '16
The place I find it more readable is in functions like
map, where something like...arr.map(thing => thing * 1000);reads as "map
thingtothing * 1000". I do agree that it can be less immediately readable or intuitive in other cases like object methods or general-use callbacks, for instance.0
u/Asmor Jul 10 '16
And that's super handy for some common transformations, like picking values of an object:
Object.keys(obj).map(key => obj[key])-1
u/Tysonzero Jul 10 '16
I think it is awesome that JS is slowly but surely adding more functional programming features, eventually it might start to approach the elegance of Haskell:
map (* 1000) arr.-1
Jul 10 '16
The syntax is just so poor.
It doesn't help that the functional / academic coders keep coming out of the woodwork to say that the syntax is more natural because it looks like calculus.
I fucking hated calculus!
2
u/cthechartreuse Jul 10 '16
Honestly, one of the biggest concerns I have in the Javascript world is that frameworks and copy paste solutions are the expected norm. I feel this person is expressing their pleasure that we are at a place where churn is selling down, but, this is just playing into the same problem we continue to have: jquery javascripters. Only instead of jquery, it's angular or react or ember or some other framework.
I think these posts would become less impactful if we shifted our focus away from library x and thought about stuff like patterns, solving business problems and learning new, long-lived computer science ideas instead.
I don't really care where a programmer learns about common answers to common problems, I only care that they LEARN them.
Ultimately, I am less interested if someone can make some random magical thing happen with D3 or three.js than I am whether they can deal with a big JSON blob coming back from a service, since their time will likely be spent more on data and business requirements than a single page of visualizations.
So, yes, tooling will always be important, but more than tools, can the programmer get sh*t done? The fact that tools have stabilized is great, but did that make the post author, or anyone else for that matter, better at their job? I think the answer is probably no.
2
1
Jul 10 '16
The language just needed a critical mass of "missing" stuff to be implemented. I bet you this leads to significantly fewer "UGH #$*$ THIS" moments that may have led to someone trying to build another abstraction layer over something because the vanilla method was awful and the most popular alternatives still just weren't quite enough.
I've been delighted at how little lodash I need and how my Webpack is under 100 lines for once. My standard scaffold for a React app seems to be getting simpler and simpler.
-3
u/tonetheman Jul 10 '16
Yeah nope... javascript is still a large interesting mess.
-3
-7
u/vagif Jul 10 '16
Thanks but no, thanks. Since we moved on to Purescript, descending back to javascript sends shivers down my spine.
1
u/moljac024 Jul 10 '16
I'd be interested to hear how you build the frontend in purescript. Have you looked into Elm? What made you choose purescript?
1
u/vagif Jul 10 '16
We code in haskell. (backend) So purescript was closer to haskell than Elm is. We use thermite (react wrapper lib).
1
u/moljac024 Jul 10 '16
Oh, I didn't realize there was a react wrapper for purescript. React really did rock the boat, eh?
What would you say are the biggest pain points in using purescript?
1
1
u/Tysonzero Jul 10 '16
As much as I do think the new JS features are cool, I agree with you that existing functional programming languages like Haskell, PureScript etc. are a million times more fun to work with.
83
u/[deleted] Jul 10 '16
Maybe this is going to sound ultra elitist (sorry) but what is with all the medium articles from people who have just started their Javascript careers? It's like every code camp encourages people to go write an article or tutorial on medium right when they're done.
I care about the opinions of some of the real amazing JS ninjas out there. I do not care about what some random guy who recently got into programming thinks about JS and web development.