Reputation: 2691
I got two functions, passing one of then as parameter, like:
var a = function(f)
{
// some code
f();
};
var b = function()
{
};
a(b); // works great, the function a is executed, then the function b is executed
Now I need to extend it to tree functions, like:
var a = function(f)
{
// some code
f();
};
var b = function(f)
{
// some code
f();
};
var c = function()
{
};
a(b(c)); // but it does not work. b(c) words like a method and get executed right on the instruction.
How can i do that?
Upvotes: 0
Views: 105
Reputation: 41256
It sounds like you want to use a callback kind of pattern. Instead of simply passing along the results of the functions, you would do something like so:
var a = function(callback)
{
// Do Stuff
callback();
}
var b = function(callback)
{
// Do Stuff
callback();
}
var c = function() { }
Your code would end up looking like so:
a(function()
{
b(function()
{
c();
});
});
Effectively, you pass along another function to execute after the method finished. In the above scenario, I simply supply two anonymous functions as the callback arguments.
Upvotes: 2
Reputation: 23142
One way would be to use an anonymous function:
a(function() { b(c); });
Upvotes: 1
Reputation: 12887
function a(f){
// code
f()
}
function b(f){
// code
a(f())
}
function c(){
//code
}
b(c);
Upvotes: 1
Reputation:
Pass a wrapper function that executes b(c)
:
a(function() {
b(c);
});
Upvotes: 3