Skip to content

Instantly share code, notes, and snippets.

@robinduckett
Last active September 28, 2017 16:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save robinduckett/6b05a21a0a36a45ab4ab to your computer and use it in GitHub Desktop.
Save robinduckett/6b05a21a0a36a45ab4ab to your computer and use it in GitHub Desktop.
// Callback Hell. You and I both know it's hell.
// Here's a secret: Escape Callback Hell...
// ... by using classes, you fucking jackass.
// This is shit:
fs.readdir('tmp', function(err, list) {
async.map(list, function(item, cb) {
cb(null, item.toUpperCase());
}, function(err, results) {
console.log(results);
});
});
// This is how it has always supposed to be done
function LoudFiles() {
fs.readdir('tmp', this.dir.bind(this));
}
LoudFiles.prototype.dir = function(err, list) {
if (err) {
return console.error(err);
}
async.map(list, this.embiggen.bind(this), this.results.bind(this));
};
LoudFiles.prototype.embiggen = function(item, callback) {
callback(null, item.toUpperCase());
};
LoudFiles.prototype.results = function(err, results) {
console.log(results);
};
var loud = new LoudFiles();
// Now, don't let me see you fuck around anymore.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment