3

I'm just trying a simple callback to get code to execute in order instead of asynchronously. This does not work:

function fn1(string, func){
    console.log("hello");
    func();
};

function fn2(){
    console.log("world");
}

fn1("meaninglessString", fn2());

It actually print "world" then "hello" to the console then crashes. But this does:

function fn1(string, func){
    console.log("hello");
    func();
};

fn1("meaninglessString", function(){
    console.log("world");
});

Do I have to always write the callback function code right in the call to fn1 or is there a way to reference an already-written function? Also, is this the best way to be doing this in Node.js if I just want one function to happen after another has finished?

3
  • 1
    remove the () from fn2 in your first example. You are calling the function, not passing it. Commented Jan 27, 2013 at 23:25
  • In your first example replace fn2() with just fn2 in the fn1 method call. fn2() will evaluate, logging "world", and then will return undefined which is passed as the callback for fn1 Commented Jan 27, 2013 at 23:25
  • Is fn1 asynchronous? You don't have to use a callback if the function you're passing it to is synchronous. You can just call the second function after calling the first one. Commented Jan 28, 2013 at 0:02

2 Answers 2

6

Take a look at your last line:

fn1("meaninglessString", fn2());

it should be the following instead:

fn1("meaninglessString", fn2);

Including the parenthesis causes fn2 to be executed immediately.

Sign up to request clarification or add additional context in comments.

2 Comments

That worked. The questions still remains though, is this the best way to control the order of execution in Node.js?
Simply put: yes. This is exactly how you should do it. Additionally, you're just not going to be able to avoid this sort of thing with node. Most of the system calls (which would probably be blocking in other languages) are asynchronus and require passing callbacks in order to do anything useful with them. howtonode.org/control-flow has some interesting example code.
2

In your first block you have:

fn1("meaninglessString", fn2());

This says "call fn1 with arguments "meaninglessString" and the result of calling fn2 with no arguments". So the interpreter does that it calls fn2 and gets nothing in return from it, it prints "world" as a biproduct it then calls fn1 with the arguments "meaninglessString" and undefined which prints "hello" and calls a function that hasn't been set(undefined) hence it crashes.

If instead you change your code to pass the function rather than the result of calling the function as below it will work as you expected

function fn1(string, func){
    console.log("hello");
    func();
};

function fn2(){
    console.log("world");
}

fn1("meaninglessString", fn2);

Comments

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.