-
-
Save bjrmatos/4a2e8618af84d4fe66df to your computer and use it in GitHub Desktop.
callback hell and async module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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