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?
Take a look at your last line:
it should be the following instead:
Including the parenthesis causes fn2 to be executed immediately.