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

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.