简体   繁体   中英

How can I direct node.js callback to a non-anonymous function

This is my working simple sample example:

function some_function(arg1, arg2, callback) {
    var my_number = Math.ceil(Math.random() *
        (arg1 - arg2) + arg2);
    callback(my_number);
}

some_function(5, 15, function(num) {
    console.log("callback called! " + num);
});

I'm trying to not use the anonymous function. This is my attempt which errors:

function some_function(arg1, arg2, callback) {
    var my_number = Math.ceil(Math.random() *
        (arg1 - arg2) + arg2);
    callback(my_number);
}

some_function(5, 15, some_other_function(num));

function some_other_function(theNumber){
    console.log('printing number: ' + theNumber);
}

Is this even possible? If so, how?

Your callback(my_number); will call some_other_function with the argument my_number .

You were very close.

function some_function(arg1, arg2, callback) {
    var my_number = Math.ceil(Math.random() *
        (arg1 - arg2) + arg2);
    callback(my_number);
}

some_function(5, 15, some_other_function);

function some_other_function(theNumber){
    console.log('printing number: ' + theNumber);
    document.write('printing number: ' + theNumber);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM