-1

If I have the following callback function:

a(function  () {
  b() 
})

my understanding is that a would be executed first, then b once a is finished

But generally in programming, you pass parameters to a function within the parantheses right? So in this function, it seems as though you are passing

function  () {
  b() 
}

to the function a as an argument. So according to what I know about programming, a should execute with all of that as an argument right?

Yet according to what I know about callbacks, it means instead, that all of that executes after the a function executes. Can you see the contradiction? Also, then wouldn't the function a be executing without parameters?

Do parameters just work differently in javascript. By default, does all the stuff in the parentheses execute after the function itself?

2
  • Does this make any difference to you if it is written as a named function instead? function myCallback () { b(); }; a(myCallback); It is just a reference, it does not execute it. Commented Mar 6, 2014 at 20:26
  • epascarello: I do agree that is the same thing. What is this supposed to illustrate? It still seems that myCallback is excecuting after a, rather than as an argument as it seems like it would be with a(myCallback) Commented Mar 6, 2014 at 21:55

1 Answer 1

5

The a() function would have to have an argument that is actually a callback, it doesn't just work automagically

function a(callback) {  // callback argument
    // do lots of stuff

    callback(); // call the function after stuff has been done
}

a(function() {
    b();
});

And you could also just do

a(b);

and pass arguments to the callback

function a(callback) {  // callback argument
    // do lots of stuff

    callback(param1, param2, param3);
}

a(function(param1, param2, param3) { // here we get them back
    b(param2); // and we can pass them along
});

This is especially useful with async behaviour

function a(callback) {  // callback argument

    $.ajax({
        // ajax arguments
    }).done(function(returned_data) {

        callback(returned_data);

    });

}

a(function(returned_data) { // data from the ajax call
     // do something with returned_data
});

That's just an example, $.ajax returns a promise that is more handy to use, but it shows how a callback would work with async functions.

As a sidenote, you'll often see code where a callback is not guaranteed, and then it makes sense to check if there was a callback function passed as an argument before trying to execute it to avoid errors.

function a(callback) {  // callback argument
    // do lots of stuff

    if (typeof callback === 'function') {
        callback();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

You can even do var b = function() { /* do stuff */ } and pass that as an argument a(b);
@Mathias - Yes you can, variables can hold function expressions, so they can be passed as arguments as well.
thank you for your fantastic answer. I have never heard of the .done method you use in your ajax example. This seems incredibly useful. Why is this not used every time with asynchronous function where you need to wait for one to finish? Is it best practice?
@BigBoy1337 - It's generally done that way, but $.ajax and some other async jQuery methods returns a "promise", and it's more clever to use that directly instead of a callback argument. It works something like this -> jsfiddle.net/B29mA/1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.