MySQL in NodeJS 是否能够等待全部 query 回传结果才继续执行
展开全部
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);
}
});
}
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);
}
});
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询