简体   繁体   中英

node.js - Does calling the callback terminate the current function

I want to call a util function that might encounter an error. In case of an error the process function should be terminated. The only way to throw an error properly is the callback function.

I would terminate the function by returning, but since I am in the util function, the process function will continue after the util() call.

function util(callback) {

    // do something synchronous

    if (err) {
        // doesn't terminate the process function
        // since we are in the util function
        return callback("something unexpected happened");
    }
}

function process(callback) {
    util(callback);
    console.log("This should not be printed if an error occurs!");
}

process(function (err) {
    if (err) {
        // this has to be executed in case of an error
        console.log(err);

        // terminate the process function somehow?
    }
});

Does calling the callback terminate the current function?

No, a callback is just a regular function. It might of course throw an exception (although that is despised).

I want to call a util function that might encounter an error. In case of an error the process function should be terminated.

For that, you need to check in the callback what did happen and act accordingly. You could use process.exit for termination.

function myProcess(callback) {
    util(function(err, result) {
         if (err) {
             callback(err);
         } else {
             console.log("This should not be printed if an error occurs!");
             callback(null, result);
         }
     });
}

myProcess(function (err) {
    if (err) {
        // this has to be executed in case of an error
        console.log(err);
        process.exit(1);
    }
});

Notice that promises could simplify this a lot, as they distinguish between success and error callbacks. util would have to return a promise for that:

function util() {
    return new Promise(function(resolve, reject) {
        // do something asynchronous

        if (err)
            reject("something unexpected happened");
        else
            resolve(…);
    });
}

function myProcess() {
    return util().then(function(res) {
        console.log("This should not be printed if an error occurs!");
        return res;
    });
}

myProcess().catch(function (err) {
    // this has to be executed in case of an error
    console.log(err);
    process.exit(1); // you might not even need this:
    throw err; // The node process will by default exit with an unhandled rejection
});

No, you will want to do something like this:

            function util(callback) {
                // do something synchronous
                if (err) {
                    throw new Error('Something unexpected happened');
                }
                callback(); // only execute callback if an error did not occur
            }

            function process(callback) {
                try{
                    util(callback);
                    console.log("This should not be printed if an error occurs!");
                } catch(error){
                    process(error);
                }
            }

            process(function (err) {
                if (err) {
                    // this has to be executed in case of an error
                    console.log(err);
                    process.exit(1)
                }
            });

I suggest you make a few changes to your code

function util(callback) {

    // do something synchronous

    if (err) {
        // doesn't terminate the process function
        // since we are in the util function
        callback("something unexpected happened");
        return false;
    }

   return true;
}

function process(callback) {
    if(!util(callback)) return;
    console.log("This should not be printed if an error occurs!");
}

This seems like a good time to use a Promise, promises are Javascript's way of forcing a synchronous function. Basically you set it up like this:

var util = new Promise(function(resolve, reject) {
    /* do some stuff here */

    if (someCondition) {
        resolve("With a message");
    } else {
        reject("with a message");
    }
}

function process() {
    util.then(function() {
        console.log("Won't call if there is an error");
    }).catch(function() {
        console.log("There was an error, Bob");
    });
}

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