【发布时间】:2018-01-31 13:04:09
【问题描述】:
//global array to store dynamodb query result
var store = [];
//function that to retrive socket data based on user email id as a hash key and give json to pass into query by taking email as input
var getSocketParams = (email) => {
return {
TableName: "socketSockets",
KeyConditionExpression: "user_id = :em",
ExpressionAttributeValues: {
":em": email
}
};
}
//query 1 which retrive all sockets details from respective table
var q1 = (callback) => {
retrivalDetail(getSocketParams(em), function(err, res) {
if (err) callback(err, null);
callback(null, res);
});
};
//query 2 take input as a result of query 1 response and second a callback
var q2 = (res, callback) => {
//here i made some logic to check callback is not executed until all items in query 1 response executed and that respective data fetched from respective table if have any
var ctr = 0;
res.Items.forEach(function(d) {
var payload_params_l = {
TableName: "socketPayload",
KeyConditionExpression: "#mac = :mac",
ExpressionAttributeNames: {
"#mac": "mac"
},
ExpressionAttributeValues: {
":mac": d.socket_id
},
ScanIndexForward: false,
Limit: 1
};
retrivalDetail(payload_params_l, function(err, res) {
if (err) callback(err, null);
store.push(res);
ctr++;
if (ctr === Object.keys(res.Items).length) {
callback(null, store);
}
});
});
};
//my function call to excute query 1 and query 2 here problem is due to asynchronous nature of node js i guess before getting query 1 result completely query2 will be start executing so i place q2 inside q1 callback here how can i retrive all data from q2 inside loop and pass all data after finishing loop to q2 callback
function call() {
q1(function(err, res) {
if (err) done(err, null);
q2(res, function(err, res) {
if (err) done(err, null);
//final function done which gives response to API request done(error ,response)
done(null, res);
});
});
};
call();
这里我面临的问题是由于节点 js 的事件循环的异步性质,我猜在完全获取查询 1 结果之前,query2 将开始执行,所以我将 q2 放在 q1 回调中我想要更好的建议来改进此代码我如何从 q2 内部循环中检索所有数据并在完成循环后将所有数据传递给 q2 回调
如果可能的话,我是在 node js 中使用 async 包的新手,使用 async 模块对这有什么帮助
【问题讨论】:
标签: node.js asynchronous amazon-dynamodb aws-lambda