node kriskowal/q promise stack errors on chaining promises

The following code errors out on the first .then with:

/Users/danielbyrne/working/mite_npm/mite.js:48
      .then(configMan.validateSettings(parsedData))
ReferenceError: parsedData is not defined

I don't understand why this code is failing. The call:

parseConfigurationFile(path.join(__dirname,'file.config'))
        .then(validateSettings(parsedData));

The functions it calls:

function parseConfigurationFile(fileName) {
    var readFile = q.nfbind(fs.readFile);   

    readFile(fileName,"utf-8")
    .then(function(data) {      
        var deferred = q.defer();        
        // Return the Config 'settings' in JSON
        deferred.resolve(JSON.parse(data));
        return deferred.promise;    
    });         
}

function vaidateSettings (data) {...}

The only way this works is if I change the function validateSettings to an anonymous function and place it inline like this :

parseConfigurationFile(path.join(__dirname,'file.config'))
        .then(function(parsedData){...});

Why can i not chain named functions in this manner?

Your validateSettings call should be like this:

parseConfigurationFile(path.join(__dirname,'file.config'))
    .then(validateSettings);

The reason is that the validateSettings needs to be referenced as a function and the .then will invoke that function with the correct parameter. Doing it the way you are doing it, you receive a reference error because parsedData is not available at the time the function call is bound.