简体   繁体   中英

How deal with the parameter 'error' in a call back function in node.js?

what is the real purpose of the error parameter in a call back function?

I've done many node.js courses, and the parameter error always appears in the same way:

something(function(error, result) {
  if(error) {
    console.log(error);
  } else {
   // do something with the result.
  }
});

This is it? I should always use a console.log(error) if the error is true? I should not send a message to the views?

Can someone show me a good example of dealing with the error parameter in a call back function??

It's difficult do describe what to do with the error as it depends on what something is.

To give you a view how it works please consider this asynchronous pattern:

something(function(error, result){
    if(error) {
        //exception handler
    } else {
        //normal execution flow
    }
});

as equivalent of synchronous pattern:

try {
    var result = something();
    //normal execution flow
} catch (error) {
    //exception handler
}

So you should do the same thing with the error as you would normally do in catch block.

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