Beginner here. I need help understanding this callback function better

90
July 02, 2019, at 08:40 AM

So, I'm learning about promises, and to demonstrate promises, the instructor is first starting with traditional callback example. However, I'm a little confused on the syntax he's using and where some extra parameters seem to have appeared from.

What I'm looking for is a dumbed down explanation to help me wrap my head around it. These are my specific questions:

  1. In the first line of the code below, is 'callback' and argument? Is it the name of the function? Or is it something else?

  2. On the third and fourth lines, I understand what we're passing in (either the error or the result), but I don't understand where those two parameters came from since we didn't define two parameters anywhere. How does the program know that's okay and not throw an error?

I don't need help with understanding when the function is called, though, because I understand that, it's just some of the stuff in defining the function that I'm confused on. I think it might be the arrow syntax that's throwing me off.

I've tried googling and rewatching the videos over and over again, but still can't grasp what's going on.

const doWorkCallback = (callback) => {
    setTimeout(() => {
       // callback('This is my error', undefined)
       callback(undefined, [1, 4, 7])
    }, 2000)
}
doWorkCallback((error, result) => {
    if(error) {
        return console.log(error)
    }
    console.log(result)
})

Thank you all in advance for your help.

Answer 1

Is 'callback' an argument?

No, it is the name of the parameter (an argument is what you pass to a function, a parameter is the variable used to access that argument from within the function).

Is it the name of the function?

Kind of. You can use that Identifier to accesss the variable (/parameter) which holds whatever value you passed to the function as an argument. In your case, you pass a function to it ((error, result) => ...), so callback will refer to that function.

On the third and fourth lines, I understand what we're passing in (either the error or the result), but I don't understand where those two parameters came from since we didn't define two parameters anywhere.

You did define a function that takes two parameters ((error, result) => {). But that does not matter: You can pass any number of arguments to a function, no matter how many parameters it has. If you pass more arguments than parameters, the arguments will go into nowhere:

   alert("used", "ignored");

If it is the other way round, parameters that don't receive an argument are undefined:

  function one(arg1) {  /* arg1 is undefined */ }
  one();

I think it might be the arrow syntax that's throwing me off.

Here is a version just with regular functions:

 function doWorkCallback(callback) {
    setTimeout(function() {
      // callback('This is my error', undefined);
      callback(undefined, [1, 4, 7]);
    }, 2000);
  }
  doWorkCallback(function (error, result) {
    if(error) {
      return console.log(error);
    }
    console.log(result);
  })

You can read about arrow functions on Codeburst and MDN ...

Answer 2

1- Yes, callback is the argument of your doWorkCallback function.

2- You are passing down a function, not two arguments. That function does receive too arguments which are error and result.

So, if you go to your doWorkCallback function you see that you will be calling wathever the callback is, undefined as error and [1, 4, 7] as result every 2 seconds

READ ALSO
Mongo DB Scalability

Mongo DB Scalability

My Node JS application with Mongoose and MongoDB receives about 15,000-45,000 posts a day

86
Extract nested <a> elements from a JSON string

Extract nested <a> elements from a JSON string

I am trying to find all "a" keys in a JSON string and save them in an arraySince they may be nested at different levels, I'm using recursion to traverse each key's children to exhaustively list all "a" keys

74
Cannot find NewsChannel module [on hold]

Cannot find NewsChannel module [on hold]

I pushed an update to heroku to finalize some updatesThe build went through fine, but when turning on the worker, I got this error

78
How to send events to a specific client using server sent events + nodejs

How to send events to a specific client using server sent events + nodejs

With web sockets it is possible to send notification based on the socket id , but using server sent events how to send a notification to a specific client ?

100