MySQL in NodeJS 是否能够等待全部 query 回传结果才继续执行

 我来答
缘Lai如茨
2016-05-30 · TA获得超过8630个赞
知道大有可为答主
回答量:7534
采纳率:92%
帮助的人:624万
展开全部
var SQLQueryCheck = {
createArrayInit: function (size) {
var arr = new Array();
for (var i = 0 ; i < size ; i++)
arr[i] = false;
return arr;
},
checkedAllQueried: function(arr) {
for (var i = 0 ; i < arr.length ; i++)
if (arr[i] === false)
return false;
return true;
}
}

var getAllCouponsInfoByManagerUidAndRange = function (uid, range, callback) {
// Select the special range in coupons, the range object have 'start' and 'amount'.
var sql_1 = "SELECT * FROM coupon WHERE coupon_id BETWEEN " + range.start + " AND " + (range.start + range.amount - 1);
dbclient.query(sql_1, function (err, coupons) {
// Fixed the properties.
coupons = TaskDateConvertToJson(coupons, new Array("create_time", "used_time"));

var queryChecked = SQLQueryCheck.createArrayInit(coupons.length);

for (var i = 0 ; i < coupons.length ; i++) {
(function (j) {
// Get the employees' user info of having received coupons.
findUserByUid(coupons[j].uid, function (employee) {
// Get the managers of creating coupons.
findUserByUid(coupons[j].manager, function (manager) {
// Get the employees' task info of having received coupons.
findTaskByTid(coupons[j].tid, function (task) {
coupons[j]['employee'] = employee;
coupons[j]['manager'] = manager;
coupons[j]['task'] = task;
delete coupons[j]['uid'];
delete coupons[j]['tid'];
queryChecked[j] = true;
if (SQLQueryCheck.checkedAllQueried(queryChecked))
callback(coupons);
});
});
});
})(i);
}
});
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式