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

29

u/[deleted] 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.

0

u/[deleted] 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).

3

u/[deleted] 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 way

Oh my god, these apply to every function call in my application! Promises are the hammers to all the nails!

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

u/[deleted] 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.

0

u/[deleted] 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

u/[deleted] 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

u/[deleted] 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.