Creating a proper Javascript Callback that takes parameters?

fuzzybabybunny

Moderator<br>Digital & Video Cameras
Moderator
Jan 2, 2006
10,455
35
91
I'm still a bit befuddled with callbacks.

Callback: a function that is run after the main function has finished running.

Code:
var mySandwich = function(param1, param2, callback) {
    alert('Started eating my sandwich. It has: ' + param1 + ', ' + param2);
    if (callback && typeof(callback) === "function") {
        callback();
    }
}

mySandwich('ham', 'cheese', 'vegetables'); // code will only alert ham and cheese.

mySandwich('ham', 'cheese', function(){ alert("hello"); }); // will alert ham and cheese and then alert a second time hello

So if I'm writing a custom function with the option for a callback, I have to:

1. specify it as a parameter
2. do a check and make sure that the parameter passed in from the user is actually a function
3. run the callback at the very end of my code block

When I'm actually running the function, I just pass in the other parameters like normal, but I type in the actual function code as the last parameter.

Obviously this won't work if the callback function I want is a big function and requires multiple parameters.

So how do I define or run my original function if my callback function is really large?

Code:
var mySandwich = function(param1, param2, callback) {
    alert('Started eating my sandwich. It has: ' + param1 + ', ' + param2);
    if (callback && typeof(callback) === "function") {
        callback();
    }
}

var superLongCallbackFunction = function(param1, param2, param3){
  // a billion lines of code
};

How do I run this callback inside of the mySandwich function?

If I do:

Code:
mySandwich("tuna", "mayo", superLongCallbackFunction("lala", "hihi", "lol"));

The superLongCallBackFunction will run FIRST because (I think) as JS parses through the parameters, it simple runs superLongCallbackFunction("lala", "hihi", "lol") when it gets to it.
 
Last edited:

nickbits

Diamond Member
Mar 10, 2008
4,122
1
81
first you syntax is wrong, it should be:
var mySandwich = function(param1, param2, callback) {
alert('Started eating my sandwich. It has: ' + param1 + ', ' + param2);
if (callback && typeof(callback) === "function") {
callback();
}
}

and what I think you want to do is:

mySandwich("tuna", "mayo",
function()
{
superLongCallbackFunction("lala", "hihi", "lol")
});

You want to pass an object (function), not the result of a function call (unless that function returns an object/function--ha!)

i suggest some tutorials on nodejs or jquery if you really want to understand what is going on.
 

fuzzybabybunny

Moderator<br>Digital & Video Cameras
Moderator
Jan 2, 2006
10,455
35
91
first you syntax is wrong, it should be:
var mySandwich = function(param1, param2, callback) {
alert('Started eating my sandwich. It has: ' + param1 + ', ' + param2);
if (callback && typeof(callback) === "function") {
callback();
}
}

and what I think you want to do is:

mySandwich("tuna", "mayo",
function()
{
superLongCallbackFunction("lala", "hihi", "lol")
});

You want to pass an object (function), not the result of a function call (unless that function returns an object/function--ha!)

i suggest some tutorials on nodejs or jquery if you really want to understand what is going on.

bah, forgot the 'function', sorry.

OHHHHHHH, gotcha. I wrap the callback function-call inside an anonymous function. Damn, clever.
 

purbeast0

No Lifer
Sep 13, 2001
53,764
6,645
126
If I do:

Code:
mySandwich("tuna", "mayo", superLongCallbackFunction("lala", "hihi", "lol"));

The superLongCallBackFunction will run FIRST because (I think) as JS parses through the parameters, it simple runs superLongCallbackFunction("lala", "hihi", "lol") when it gets to it.

that is wrong. it will run the superlongcallbackfunction when it hits the code in the mysandwich function. it will not run it when it parses the params.

however you declared it wrong. it should be...

Code:
var superLongCallbackFunction = function(param1, param2, param3){
  // a billion lines of code
};
 

fuzzybabybunny

Moderator<br>Digital & Video Cameras
Moderator
Jan 2, 2006
10,455
35
91
that is wrong. it will run the superlongcallbackfunction when it hits the code in the mysandwich function. it will not run it when it parses the params.

however you declared it wrong. it should be...

Code:
var superLongCallbackFunction = function(param1, param2, param3){
  // a billion lines of code
};

Yeah, I know. Corrected.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,816
75
To answer your next question (the one you haven't asked yet), it looks like you're getting close to a point where you'll want to learn about closures. Your next question being somewhere along the lines of "The variables I pass to my callbacks seem to keep changing!"