0

In javascript, one is used to either do callbacks or return promises for async functions... I was just wondering, how does this work with the parameters inside the callbacks or promises, I mean one often sees things like this:

function (error, response, body)  {..}

Is the order of callback arguments defined here? Or otherwise said: Does javascript somehow "know" about what an "error" parameter and anything else is? Are there reserved keywords in javascript for e.g "error | err | e", etc.?

e.g I have seen this code here:

const bcrypt = require('bcrypt');
const password = 'Top Secret'; 

bcrypt.hash(password, 10, (err, hash) => {
  if (err) {
    throw err;
  }
  console.log('Your hash: ', hash);
});

Where "err" is first argument, the data is second, but then:

bcrypt.hash(password, 10).then(
  hash => {
    console.log('Your hash: ', hash);
  },
  err => {
    console.log(err);
  }
);

Where apparently hash is first argument in the promise, and err is second parameter, so my question is: How does javascript in general know what the error and other arguments are? Are there docs or guidelines about that?

MMMM
  • 3,320
  • 8
  • 43
  • 80
  • 1
    No. Callbacks are normal functions. Which arguments the function receives and in which order is determined by the caller of the function. However, Node.js does have the convention to pass error objects as first argument. – Felix Kling Oct 11 '18 at 06:45
  • The signature of `.then()` is `.then(successFn, errorFn)`. In the last example above, `hash` and `err` are both the first argument of their respective functions. You could use whatever member names you like, eg `donald_duck` and `miss_marple` (but please don't) – Roamer-1888 Oct 11 '18 at 07:00

1 Answers1

3

Is the order of callback arguments defined here?

Yes: it is given in the documentation of the request function, as that is what it calls the callback with.

Or otherwise said: Does javascript somehow "know" about what an "error" parameter and anything else is? Are there reserved keywords in javascript for e.g "error | err | e", etc.?

No. The language does not give special meaning to any parameter names.

"err" is first argument, the data is second

That's just a (very common) convention, documented at Why does node prefer error-first callback?, https://docs.nodejitsu.com/articles/errors/what-are-the-error-conventions/ or http://nodeguide.com/style.html#callbacks.

apparently hash is first argument in the promise, and err is second parameter

No, that's just the promise then method taking two callbacks, one for the success and one for the error case. The names of the parameters (or whether there are any parameters at all) are irrelevant.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • ah thanks good explanation, ok it is basically just the "then" function signature (resolve,reject) whereas in node is the other way around... somehow a bit confusing stuff – MMMM Oct 11 '18 at 07:26
  • @user2883596 It's not "the other way round". The signature of `then` is `(onFulfillment, onRejection?)` - one or two callbacks with one argument - while the node signature is `onFinish` - one callback with two arguments, the first being not optional. – Bergi Oct 11 '18 at 07:29