Created
April 7, 2015 16:13
-
-
Save dekobon/a650f6e218a81dead7f1 to your computer and use it in GitHub Desktop.
Returning from callbacks
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
var fs = require('fs'); | |
String.prototype.endsWith = function (suffix) { | |
return this.indexOf(suffix, this.length - suffix.length) !== -1; | |
}; | |
exports.ls1 = function ls1(dir, ext, callback) { | |
try { | |
fs.readdir(dir, function (err, files) { | |
if (err) return callback(err, null); | |
var filtered = files.filter(function (element) { | |
return element.endsWith('.' + ext); | |
}); | |
return callback(err, filtered); | |
}); | |
} | |
catch (err) { | |
callback(err, null); | |
} | |
}; | |
// VERSUS | |
exports.ls2 = function ls2(dir, ext, callback) { | |
try { | |
fs.readdir(dir, function (err, files) { | |
if (err) callback(err, null); | |
var filtered = files.filter(function (element) { | |
return element.endsWith('.' + ext); | |
}); | |
callback(err, filtered); | |
}); | |
} | |
catch (err) { | |
callback(err, null); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment