mardi 24 février 2015

nodejs : is there a way to overcome the callback if(err) boilerplate?

As you project grows, we started to have this much appreciated, defensive code snippet pretty much everywhere :



func(err, result){
if(err){

console.log('An error occurred!, @myModule :' + err);
return callback(err);
}

//then the rest..
}


A quick google search reveals some libs that attempt to overcome this common concern, (e.g. http://ift.tt/1acs3FZ)


But what is the best approach to minimize the boilerplate coding without compromising the early error handling mechanism we have?


PS: I hope this is not an off-topic, too broad of a question, if you find it so, please let me know why, before bashing it with your downvote and I will try to improve it. Thanks


Aucun commentaire:

Enregistrer un commentaire