user3519807
user3519807

Reputation: 133

"callback" keyword in JavaScript

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.

Upvotes: 11

Views: 9153

Answers (2)

Katana314
Katana314

Reputation: 8610

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.

Upvotes: 6

Sonofkyuss
Sonofkyuss

Reputation: 59

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

Upvotes: 2

Related Questions