Node.js - why is the callback getting passed redundant-looking arguments?

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) {


They don’t get used twice.It just happens that variables have the same name as parameters in the function definition.The function doesn’t get invoked until line 23

1 Like

Oh, of course.

// Here I declare my variable(s)
//Here I define my function(s)
//Here I invoke my (first) function

Not sure why I wasn’t getting that at first.


1 Like