11

Please help in understanding the below code:

// define our function with the callback argument

function some_function(arg1, arg2, callback) {
    // this generates a random number between
    // arg1 and arg2

    var my_number = Math.ceil(Math.random() * (arg1 - arg2) + arg2);

    // then we're done, so we'll call the callback and
    // pass our result
    callback(my_number);
}

// call the function
some_function(5, 15, function(num) {
    // this anonymous function will run when the
    // callback is called
    console.log("callback called! " + num);
});

In the above code,what is the callback keyword.what is the use of this word. Even there is no function defined with name callback.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user3519807
  • 133
  • 1
  • 9
  • 2
    it's just a function "pointer". e.g. `foo = function() { alert('hi mom!'); }; some_function(5, 15, foo)` – Marc B Sep 30 '15 at 19:40

2 Answers2

6

The gap in logic I think you're having a hard time with is anonymous, unnamed functions. Once upon a time, all functions were named. So code was written like this:

function MemberProcessingFunction() {
  // etc
}

function AdminProcessingFunction() {
  // etc
}

var loginProcessingFunction;

if (usertype == 'BasicMember') {
  loginProcessingFunction = MemberProcessingFunction;
}
else if (usertype == 'Admin') {
  loginProcessingFunction = AdminProcessingFunction;
}
loginProcessingFunction();

Someone thought "This is dumb. I'm only creating those function names to use them in one place in my code. Let's merge that together."

var loginProcessingFunction;

if (usertype == 'BasicMember') {
  loginProcessingFunction = function() {
    // etc
  };
}
else if (usertype == 'Admin') {
  loginProcessingFunction = function() {
    // etc
  };
}
loginProcessingFunction();

This especially saves a lot of time when you're passing a function to another function as an argument. Often, this is used for "callbacks" - occasions where you want to run certain code, but only after a certain indeterminately-timed function has finished its work.

For your top function, callback is the name of the third argument; it expects this to be a function, and it is provided when the method is called. It's not a language keyword - if you did a "find/replace all" of the word "callback" with "batmanvsuperman", it would still work.

Katana314
  • 8,429
  • 2
  • 28
  • 36
  • So, if 'callback' is not a keyword, and I can write anything I like, can I also leave it blank? – Cornelius Mar 14 '18 at 21:45
  • @Cornelius The point of having it as a variable is that in most cases you will call it at a later point in the code. You can skip having that callback if you're not going to use it. I think your question doesn't quite recognize why you would have callbacks to being with. – Katana314 Mar 15 '18 at 16:00
  • So, you are storing a function into a variable, passing it as a parameter and calling that parameter a callback? – Cornelius Mar 16 '18 at 02:05
  • @Cornelius Yes, that's a good summary. Often the variable portion is skipped, and the function is written right into the arguments. – Katana314 Mar 16 '18 at 15:56
2

'callback' is a function passed as an argument to the 'some_function' method. It is then called with the 'my_number' argument.

when 'some_function' is actually being called as seen below

// call the function
some_function(5, 15, function(num) {
    // this anonymous function will run when the
    // callback is called
    console.log("callback called! " + num);
});

it receives the whole definition of the third function argument. so basically your 'callback' argument has this value below

function(num) {
    // this anonymous function will run when the
    // callback is called
    console.log("callback called! " + num);
}

To understand better how a function can be passed as an argument to another function, take a look at this answer.

Community
  • 1
  • 1
Sonofkyuss
  • 59
  • 9
  • but when the function will be called some_function(5, 15, function(num) { // this anonymous function will run when the // callback is called console.log("callback called! " + num); }); – user3519807 Oct 02 '15 at 14:33
  • The first part is the definition: function some_function(arg1, arg2, callback) {...} Then the second part is the actual callback of the function: some_function(5, 15, function(num) {...}); I think the part that was difficult for you to understand is the 3rd argument in the callback, after 5, 15. This is ANOTHER function and the person who wrote this defined the function DIRECTLY AS A PARAMETER, as part of the callback arguments. Does that make sense? – Sonofkyuss Oct 04 '15 at 12:26
  • Another way to explain it; here is another way to write the whole code block you originally posted (does exactly the same thing): `function some_function(arg1, arg2, callback) { // this generates a random number between // arg1 and arg2 var my_number = Math.ceil(Math.random() * (arg1 - arg2) + arg2); // then we're done, so we'll call the callback and // pass our result callback(my_number); } function callbackArgFunct(num) { console.log("callback called! " + num); } some_function(5, 15, callbackArgFunct);` – Sonofkyuss Oct 04 '15 at 12:48
  • cheers. reminds me of this keyword in python/django and the call would be something like this.run – Mr-Programs Nov 04 '18 at 01:55