Why, on line 23 below, does getFiles get passed the filterStr? I see how filterStr gets used higher up in the .filter fuction. But if list is already populated during fs.readdir, why does the callback further down need filterStr again? I guess the same could be asked about dir as well. I just see getFiles either returning an error or printing the contents of list to the console.
var fs = require('fs')
var path = require('path')
var dir = process.argv[2] // '/tmp/_learnyounode_2995'
var filterStr = process.argv[3] // "md"
function getFiles(dir, filterStr, callback) {
fs.readdir(dir, function (err, list) {
if (err)
return callback(err)
list = list.filter(function (file) {
return path.extname(file) === '.' + filterStr
})
callback(null, list)
})
}
getFiles(dir, filterStr, function (err, list) {
if (err)
return console.error('There was an error:', err)
list.forEach(function (file) {
console.log(file)
})
})