r/javascript • • Oct 06 '16

Derick Bailey: Callbacks First, Then Promises

https://derickbailey.com/2016/10/05/callbacks-first-then-promises/
1 Upvotes

16 comments sorted by

5

u/This_Is_A_Robbery Oct 06 '16 edited Oct 06 '16

You can hem and haw over how much you dislike promises, but the fact of the matter is that if you want to do nodeJs development in a professional environment you won't even get in the door if you don't have Promises on your resume.

Purely practically, callbacks are ugly, require too much boiler plate code, require strict adherence to an arbitrary and bad function pattern and SWALLOW ERRORS LIKE TICK TACKS. And bluebird is FASTER than the async library!

Let's look at that Nolan Lawson article he mentioned. Here's a relevant quote from it:

Promises are great. If you are still using callbacks, I strongly encourage you to switch over to promises. Your code will become smaller, more elegant, and easier to reason about.

This is a bad article, with bad advice. Do not use callbacks if you can avoid it, and you can always (or nearly, at least) avoid it.

1

u/[deleted] Oct 06 '16

"Promises on your resume".

lol wut?

5

u/jcready __proto__ Oct 06 '16 edited Oct 06 '16

He starts that post with a question, asking you to explain the difference in four uses of a promise.

doSomething().then(function () {
  return doSomethingElse();
});

doSomething().then(function () {
  doSomethingElse();
});

doSomething().then(doSomethingElse());

doSomething().then(doSomethingElse);

This, to me, demonstrates the primary reason that I don’t reach for promises by default. There are a lot of potential mistakes to make.

Wat?

Please explain the difference in four uses of a callback.

doSomething(function (err, callback) {
  callback(err, doSomethingElse());
});

doSomething(function (err, callback) {
  doSomethingElse();
  callback(err)
});

doSomething(doSomethingElse());

doSomething(doSomethingElse);

This, to me, demonstrates the primary reason that I don’t reach for callbacks by default. There are a lot of potential mistakes to make.

3

u/arcanin 🧶 Yarn maintainer Oct 06 '16

article tl;dr:

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).

0

u/[deleted] Oct 06 '16

[deleted]

4

u/This_Is_A_Robbery Oct 06 '16 edited Oct 06 '16

What reasons?

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.

don't use callbacks.

3

u/jcready __proto__ Oct 06 '16

Well for one thing you cannot await a function which uses a callback in an async function. Where as Promises are the only thing which can be awaited.

-2

u/bradleymeck Oct 06 '16

see galaxy or generator-runner off npm (even co has examples of this), use a promisify function, there are plenty of ways to do this:

async function () {
  const txt = await promisiefy(fs.readFile)('thing.txt');
}

3

u/jcready __proto__ Oct 06 '16

Those are all returning promises (or rather, "thenables"). I'm not sure what your point is.

-2

u/bradleymeck Oct 06 '16

That you cannot await a function which uses a callback in an async function is false.

3

u/jcready __proto__ Oct 06 '16

Let's review what I said:

Well for one thing you cannot await a function which uses a callback in an async function.

So I said the following wouldn't work:

function doubleUsingCallbacks (value, callback) {
  setTimeout(() => callback(null, value * 2), 500)
}

async function asyncQuadruple (value) {
  const doubled = await doubleUsingCallbacks(value)
  return doubled * 2
}

Where as promises are the only things which can be awaited.

So I said the following would work:

function doubleUsingPromises (value) {
  return new Promise((resolve) =>
    setTimeout(() => resolve(value * 2), 500)
  )
}

async function asyncQuadruple (value) {
  const doubled = await doubleUsingPromises(value)
  return doubled * 2
}

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?

-3

u/bradleymeck Oct 06 '16

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.

3

u/jcready __proto__ Oct 06 '16

I'm unsure how to debate an idiot so I will end the conversation here.

-2

u/bradleymeck Oct 06 '16

The argument that you cannot await the return value of a function that takes a callback is very different from what you stated ;)

2

u/arcanin 🧶 Yarn maintainer Oct 06 '16

Is it though? What reasons? Who decided this?

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.

-1

u/pkstn Oct 06 '16

My thoughts exactly! 👍

0

u/jdlshore Oct 07 '16

A surprising amount of butthurt in this thread.