One issue is that getVersionInFile()
is returning a value before the asynchronous readFile()
has finished (also being async, readFile()
does not return a meaningful value). Also, using a limit/concurrency of 1 for forEachLimit()
is the same as forEachSeries()
. Here is an example that uses mapSeries()
that should get you the same end result:
exports.getAllVersionInformation = function(request, response) {
if (!utilities.checkLogin(request, response))
return;
// Get the array of file information stored in the config.js file
var fileCollection = config.versionsArray;
async.mapSeries(fileCollection, function(fileInformation, callback) {
// Retrieve all information particular to the given file
var name = fileInformation[0];
var fullPath = fileInformation[1];
var lineNumber = fileInformation[2];
var startIndex = fileInformation[3];
var endIndex = fileInformation[4];
// Get the version number in the file
getVersionInFile(fullPath,
lineNumber,
startIndex,
endIndex,
function(error, version) {
if (error)
return callback(error);
callback(null, { name: name, version: version });
});
}, function(error, responseObjects) {
if (error)
return console.log('There was an error: ' + error);
// Respond with the JSON representation of the response array
response.json(responseObjects);
});
};
function getVersionInFile(fullPath, lineNumber, startIndex, endIndex, callback) {
fs.readFile(fullPath,
{ encoding: 'utf8' },
function(error, file) {
if (error)
return callback(error);
var lineArray = file.split('\n');
version = lineArray[lineNumber].substring(startIndex,
endIndex + 1);
callback(null, version);
});
};