mercredi 25 novembre 2015

What is best practice for asynchronous javascript error handling?

I am interested what is considered best practice for handling errors from an asynchronous JavaScript function. I have given two examples below one which handles the error in an if statement and one which handles the success in the if statement. Previously I have always handled the error in the if statement but wondered whether this was the best approach. Please let me know which you consider to be the best approach and why?

function(err, res){
    if(err){
       // handle error
       return;
    }
    // code to handle response
}

or

function(err, res){
    if(!err){
       // code to handle response
       return;
    }
    // handle error
}

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire