1

I have had trouble understanding the rules concerning callbacks in javascript. I understand that callbacks run after function x finishes however I find there is ambiguity when defining them.

In the node.js docs : https://nodejs.org/en/knowledge/getting-started/control-flow/what-are-callbacks/

The code

function processData () {
  var data = fetchData ();
  data += 1;
  return data;
}

is changed to

function processData (callback) {
  fetchData(function (err, data) {
    if (err) {
      console.log("An error has occurred. Abort everything!");
      return callback(err);
    }
    data += 1;
    callback(data);
  });
}

when the anonymous function is created why can we use parameters, where do these arguments come from, what rules regard these parameters?

The context of this question comes from the sockets.io library Specifically:

var io = socket(server);
io.on('connection', function(socket){}

Why can we reference the socket, can I just add in function(random_param, socket)? What tells the function to reference when passing random_param?

I was told read the docs, which I had already done but that didn't make things any clearer.

Thanks in advance.

3
  • 3
    "I understand that callbacks run after function x finishes..." Not necessarly. Not forEach's callback, for instance, or map's, or sort's, or... Commented May 23, 2020 at 17:28
  • 1
    The internal code of the function io.on(eventName, callback) determines what is passed to that callback when it is called. You can't randomly change it Commented May 23, 2020 at 17:31
  • There are no special "rules" concerning callbacks. In fact, the common term "callback" is just a conventional name that isn't always appropriate. All that's happening is that functions are "first class" values in JS and so can be passed as arguments to other functions. fetchData and io.on clearly expect functions as those parameters, and presumably document how and when they will be called, including with which arguments. Commented May 23, 2020 at 17:34

1 Answer 1

2

I understand that callbacks run after function x finishes...

Not necessarily. The JavaScript standard library (and various others) have lots of non-asynchronous callbacks. Think of the callbacks on the array functions forEach, map, sort...

...when the anonymous function is created why can we use parameters, where do these arguments come from, what rules regard these parameters?

They come from the code calling the callback. In your case, code in the socket.io library you're using calls the callback with two arguments, which your callback receives in the parameters err and data.

Here's a somewhat silly example: A function that calls a callback with a random number:

// This is analogous to the code in the socket.io library
function getARandomNumberWithRandomDelay(callback) {
    setTimeout(() => {
        callback(Math.random());
    }, Math.random() * 1000);
}

// This is analogous to your code using the socket.io library
getARandomNumberWithRandomDelay(function(num) {
    console.log("I got the number " + num);
});

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.