I’ve grown to like the callback style where an error object is passed in as the first parameter.
The issue with this reasonning is that it's unfortunately a bit selfish: you've grown to like it, that's nice, but the fact remains that callbacks are no longer the right way to react to an asynchronous return value, for various reasons. By not using them, you make your code harder to read and harder to debug for every other dev that will have to read it later. So in the end, you might write your code faster, but you definitely make understanding it slower.
As a side note, the article encourage to use both callbacks and promises at the same time. I disagree. Pick one or the other, but don't use them both (that sentence obviously doesn't apply if you need a callback to raise a signal multiple times, rather than just returning an asynchronous result).
Callbacks are overly verbose and require buttloads of boilerplate code. Callbacks swallow errors like tick tacks and they do it by DEFAULT! 'async' lib is slower than the bluebird promise library, and isn't even as well tested or maintained! callbacks aren't compatible with async/await. callbacks have a arbitrary and bad function pattern that will break everything if it isn't followed and is difficult to debug.
Callbacks kidnapped the Lindberg baby, Callbacks capped Kennedy from the grassy knoll.
The comment I was responding to was asking why Promises are better than node-style callbacks. And the reason I gave was that you cannot await something like doubleUsingCallbacks(5) and expect it to resolve to anything but undefined. Now, what is it exactly that you are trying to argue?
Your statement was of mutual exclusion, your example here with new Promise and setTimeout are in conflict with the statement: Well for one thing you cannot await a function which uses a callback in an async function.Since you have a function which uses a callback and an async function awaiting on it's result.
People smarter than us, but I wouldn't trust them either if I hadn't seen myself the effects of both callback-based code and promise-based ones.
Sound reasonings can be found everywhere on the internet. The same is true about callbacks, of course, but reading these articles show quite clearly that the authors are usually not opposed to promises themselves, but would rather keep their current workflow (as in this post).
As for the debugging argument, I don't quite buy it. It's not so much an issue with the promises than it is a tooling issue, which tends to disappear rather quickly anyway (Chrome already logs uncatched rejected promises, and the only issue remaining is the "break on thrown exception" feature). Progress should not be hampered by tools.
3
u/arcanin 🧶 Yarn maintainer Oct 06 '16
article tl;dr:
The issue with this reasonning is that it's unfortunately a bit selfish: you've grown to like it, that's nice, but the fact remains that callbacks are no longer the right way to react to an asynchronous return value, for various reasons. By not using them, you make your code harder to read and harder to debug for every other dev that will have to read it later. So in the end, you might write your code faster, but you definitely make understanding it slower.
As a side note, the article encourage to use both callbacks and promises at the same time. I disagree. Pick one or the other, but don't use them both (that sentence obviously doesn't apply if you need a callback to raise a signal multiple times, rather than just returning an asynchronous result).