0

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?

asked Apr 13, 2012 at 17:44

4 Answers 4

3

Pass a wrapper function that executes b(c):

a(function() {
 b(c);
});
answered Apr 13, 2012 at 17:47
Sign up to request clarification or add additional context in comments.

Comments

2

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.

answered Apr 13, 2012 at 17:47

Comments

1
function a(f){
 // code
 f()
}
function b(f){
 // code
 a(f())
}
function c(){
 //code
}
b(c);
answered Apr 13, 2012 at 17:47

Comments

1

One way would be to use an anonymous function:

a(function() { b(c); });
answered Apr 13, 2012 at 17:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.