[英]Async issues for multiple POST request
I use a function _getLogFileUrls
that does a POST
request and returns the data to a callback function. 我使用一个_getLogFileUrls
函数执行POST
请求,并将数据返回给回调函数。 The returned data is being added to an array. 返回的数据正在添加到数组中。
Function _getLogFileUrls
is called in a loop. 函数_getLogFileUrls
循环调用。
How do i return the resultant array after all the requests have been completed? 完成所有请求后,如何返回结果数组?
I understand setTimeout
is a wrong way for this approach and the way i am writing the code is creating a callback hell. 我知道setTimeout
是这种方法的错误方法,而我编写代码的方法正在创建回调地狱。 How do i get rid of the setTimeout
function without getting into all sorts of async issues. 我如何摆脱setTimeout
函数而又不陷入各种异步问题。
// app.post('/api/getLogs', auth, loggingAPI.getLogs); // Make API request
function getLogs(req, res) {
if ( ! req.body.id) {
return res.status(500).send('Please check the params!');
}
var date;
if (req.body.date) {
date = req.body.date;
} else {
date = new Date().toISOString().slice(0,10);
}
var sqlQuery = "SELECT `LogFileID` FROM `logs_data` WHERE `EmpID` = '" + req.body.id + "' AND DATE(`Timestamp`) = '" + date + "'",
resArray= [];
hitThisQueryForMe(sqlQuery, res, function(rows) {
if ( ! rows.length) res.json(rows);
_.each(rows, function(item) {
console.log('item: ' + item.LogFileID);
_getLogFileUrls(item.LogFileID, function(response) {
resArray.push(response);
});
});
setTimeout(function() {
console.log('final urls: ' + JSON.stringify(resArray));
res.send(resArray);
resArray = [];
}, 4000);
});
}
function _getLogFileUrls(logFileId, callback) {
var request = require('request'),
config = require('../../config.js');
var fileParams = {
fileName: 'LogFiles/' + logFileId
};
request.post({
url: config.filesServiceUrl + 'get-logfile-urls',
json: fileParams
}, function(error, response, body) {
if (!error && response.statusCode === 200) {
callback(body);
} else {
console.log('err requesting logs: ' + JSON.stringify(error));
res.status(400).send('Err requesting logs:');
}
}).on('error', function(err) {
console.log('File service error for Logs: ' + err);
});
}
Use async.eachOf to iterate over your array and apply an async function on each element: 使用async.eachOf遍历数组并在每个元素上应用async函数:
async.eachOf(myArray, function(myElementInArray, it, callback){
// call async function on element
myAsyncFunction(myElementInArray, function(err){
if(err)
return callback(err); // abort async flow
else
return callback();
});
}, function(err){
// final callback called when the flow is finished
});
//// EDIT ////编辑
async module documentation : https://caolan.github.io/async/docs.html 异步模块文档: https : //caolan.github.io/async/docs.html
Replace this code : 替换此代码:
_.each(rows, function(item){
console.log('item: ' + item.LogFileID);
_getLogFileUrls(item.LogFileID, function(response){
resArray.push(response);
});
});
setTimeout(function(){
console.log('final urls: ' + JSON.stringify(resArray));
res.send(resArray);
resArray = [];
}, 4000);
With 用
var async = require("async");
var resArray = [];
async.eachOf(rows, function(item, it, callback){
console.log('item: ' + item.LogFileID);
// call async function on element
_getLogFileUrls(item.LogFileID, function(response){
resArray.push(response);
return callback();
});
}, function(err){
// final callback called when the flow is finished
console.log('final urls: ' + JSON.stringify(resArray));
res.send(resArray);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.