Skip to content

Instantly share code, notes, and snippets.

@bjrmatos
Forked from robinduckett/callbackHell.js
Last active August 29, 2015 14:06
Show Gist options
  • Save bjrmatos/4a2e8618af84d4fe66df to your computer and use it in GitHub Desktop.
Save bjrmatos/4a2e8618af84d4fe66df to your computer and use it in GitHub Desktop.
callback hell and async module
// 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) {
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