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.
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.
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!
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.
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.
29
u/[deleted] Jul 10 '16
Callbacks obsolete? Seems premature to say so.