How do you prevent/fix callback hell?

callback hells xss

The three ways to prevent/fix callback hell are:

  • Handle every single error
  • Keep your code shallow
  • Modularize – split the callbacks into smaller, independent functions that can be called with some parameters then joining them to achieve desired results.

Also See: How can you secure your HTTP cookies against XSS attacks?

The first level of improving the code above might be:

var logError = function(error){
if(!error){
console.log(“success!!”);
}else{
console.log(“error”);
}
},
updateTransaction = function(t){
query(“UPDATE transactions SET value = ” + (t.value*0.1) + ” WHERE id=” + t.id, logError);
},
handleTransactions = function(transactions){
transactions.each(updateTransaction);
},
handleClient = function(id){
query(“SELECT * FROM transactions WHERE clientId=” + id, handleTransactions);
};

query(“SELECT clientId FROM clients WHERE clientName=’picanteverde’;”,handleClient);

You can also use Promises, Generators and Async functions to fix callback hell.

Leave a Reply

Your email address will not be published. Required fields are marked *