Javascript Node.js使用async-如何正确地将async.forEachLimit与fs.readFile一起使用?
我正在开发Node.js应用程序的一部分,该应用程序需要从各种文件中的特定点获取版本信息。我已经使用npm包async为此编写了功能代码 我很清楚我的问题是什么。然而,由于我对Node.js非常陌生,甚至对异步包也比较新,所以我没有实现正确的东西 似乎版本变量的内容没有及时响应。换句话说,响应是在版本能够发送到响应之前发送的 以下是相关代码:Javascript Node.js使用async-如何正确地将async.forEachLimit与fs.readFile一起使用?,javascript,node.js,asynchronous,file-io,package,Javascript,Node.js,Asynchronous,File Io,Package,我正在开发Node.js应用程序的一部分,该应用程序需要从各种文件中的特定点获取版本信息。我已经使用npm包async为此编写了功能代码 我很清楚我的问题是什么。然而,由于我对Node.js非常陌生,甚至对异步包也比较新,所以我没有实现正确的东西 似乎版本变量的内容没有及时响应。换句话说,响应是在版本能够发送到响应之前发送的 以下是相关代码: exports.getAllVersionInformation = function(request, response) { if (!uti
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;
// Declare the array to be used for the response
var responseObjects = [];
async.forEachLimit(fileCollection, 1, function(fileInformation, taskDone) {
// Declare an object to be used within the response array
var responseObject = new Object();
// 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
var version = getVersionInFile(fullPath, lineNumber, startIndex,
endIndex, taskDone);
console.log('Ran getVersionInFile()');
// Set the name and version into an object
responseObject.name = name;
responseObject.version = version;
// Put the object into the response array
responseObjects.push(responseObject);
console.log('Pushed an object onto the array');
}, function(error) {
console.log('Entered the final');
if (error == null)
// Respond with the JSON representation of the response array
response.json(responseObjects);
else
console.log('There was an error: ' + error);
});
};
function getVersionInFile(fullPath, lineNumber, startIndex, endIndex, taskDone) {
console.log('Entered getVersionInFile()');
var version = fs.readFile(fullPath,
function(error, file) {
if (error == null) {
console.log('Reading file...');
var lineArray = file.toString().split('\n');
version = lineArray[lineNumber].substring(startIndex,
endIndex + 1);
console.log('The file was read and the version was set');
taskDone();
} else {
console.log('There was a problem with the file: ' + error);
version = null;
taskDone();
}
});
console.log('Called taskDone(), returning...');
return version;
};
我尝试过使用getVersionInFile函数如何返回数据。我已经移动了taskDone()函数,看看这是否会有所不同。我问过谷歌很多关于异步的问题,以及在我的上下文中如何使用异步。我似乎无法让它工作
我使用过的一些更重要的资源如下:
我添加了console.log语句来跟踪代码流。下面是这方面的图片:
此外,我还有我期待的部分回复。这也是:
![浏览器输出]
这个输出的问题是JSON中的每个对象都应该有一个版本值。因此,JSON应该类似于:
[{“名称”:“WebSphere”,“版本”:“x.x.x.x”},{“名称”:“Cognos”,“版本”:“x.x.x.x”}]
如何让getVersionInFile()函数及时正确地给出版本号?另外,如何确保在不执行任何阻塞的情况下异步执行此操作(为什么使用异步进行流控制)
任何见解或建议都将不胜感激。一个问题是
getVersionInFile()
在异步readFile()
完成之前返回一个值(也是异步的,readFile()
不会返回有意义的值)。另外,对forEachLimit()
使用1的限制/并发性与forEachSeries()
相同。下面是一个使用mapSeries()
的示例,它应该可以得到相同的最终结果:
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);
});
};
你搞定了。继续当老板。非常感谢。