JavaScript Code-Segment: PiggybackData : The Proper Object-Oriented Way to Overcome Known Problem Of “Callback-Hell” And Chained Callbacks With Single Data Parameter

  /**
   * this is a proper object-oriented way to resolve a problem known as (or derived from..) "callback hell", 
   * essentially this allows information to be added to a simple string, while still maintaining its original functionality.
   * @type {{}}
   * @author icompile.eladkarako.com
   */
  String.prototype.piggybackData = {};

at any point you can store data in a string and pass it along the callback,

  /**
   * @param {function(string)?} callback
   */
var action = function(callback){
  var str = "hi";
  
  str.piggybackData.date = new Date();
  str.piggybackData.isTrue = true;
  str.piggybackData.hello = "world";

  callback(str);
};

and extract it at any time later,

action(function(strContent){
  var 
    date = strContent.date,
    isTrue = strContent.isTrue,
    hello = strContent.hello;
});

in short: JavaScript. Rocks!

p.s.
nodeJs too..

Leave a Reply