在异步调用完成之前,我遇到了分配值的问题,所以我选择使用Promises(在这个例子中:Bluebird)。我认为可以解决这个问题,但看到我完全不熟悉承诺/节点,它仍然在做同样的事情。
我如何修改下面的代码,正确使用promises?并维护流量,因此我可以根据之前的modules
?
row
var Promise = require('bluebird');
var db = require('../shared/db');
var queryHelper = require('../shared/queryHelper');
var schemas = require('../schemas/schemas');
var _ = require('lodash');
var serverSettings = require('../shared/coreServerSettings');
// Create new instance of mysql module
var connection = new db();
var queryhelper = new queryHelper();
// Promised methods
var queryAndWait = Promise.promisify(connection.query);
.. ..剪断
queryAndWait(query + '; ' + queryFoundRows, params).then(function (result) {
var payload = [];
var site_access = [];
var module_access = [];
_.each(result[0], function (row) {
var sites = row.site_access.split(',');
_.each(sites, function (site_id) {
site_access.push(site_id);
});
row.site_access = site_access;
site_access = [];
queryAndWait(queryModules, [row.priv_id]).then(function (result) {
_.each(result, function (module) {
var modulePriv = {
priv_name: module.priv_name,
priv_num: module.priv_num,
enabled: module.enabled
};
module_access.push(modulePriv);
});
//console.log("am assigning " + JSON.stringify(module_access));
row.module_access = module_access;
module_access = []
});
payload.push(row);
});
});
澄清:
抱歉,模块是用户(行)可以访问的产品组,它们是即时计算的(按位 - 因此没有可用的连接和单独的查询),模块查询返回类似下面的内容,然后我在返回有效载荷之前循环并附加到行:
[{"priv_name":"INTERACT","priv_num":1,"enabled":1},{"priv_name":"STAFF_ADMIN","priv_num":32,"enabled":1},{"priv_name":"INT_EDIT","priv_num":64,"enabled":0},{"priv_name":"FAILED_LOGIN","priv_num":128,"enabled":0},{"priv_name":"INT_TAGS","priv_num":256,"enabled":0},{"priv_name":"NC","priv_num":512,"enabled":0},{"priv_name":"CC_TIMETABLE_MGR","priv_num":1024,"enabled":0}]
答案 0 :(得分:1)
是的,内部queryAndWait
将为每一行独立运行,因为您不链接承诺。
由于你使用bluebird,你需要这样的东西来维持流程
queryAndWait(query + '; ' + queryFoundRows, params).then(function (result) {
return Promise.map(result[0], function (row) {
var site_access = [];
var module_access = [];
var sites = row.site_access.split(',');
_.each(sites, function (site_id) {
site_access.push(site_id);
});
row.site_access = site_access;
return queryAndWait(queryModules, [row.priv_id]).then(function (result) {
_.each(result, function (module) {
var modulePriv = {
priv_name: module.priv_name,
priv_num: module.priv_num,
enabled: module.enabled
};
module_access.push(modulePriv);
});
row.module_access = module_access;
return row;
});
});
}).then(function(payload) {
console.log(payload);
}).catch(function(error) {
console.log(error);
});
答案 1 :(得分:1)
Promise帮助您以简单的方式控制应用程序的流量。我鼓励你阅读有关承诺的基本原则,至少是Promise A +规范。
您的代码可以通过多种方式编写,这是我推荐的方式,因为它是模块化的:
var Promise = require('bluebird');
var queryAndWait = Promise.promisify(connection.query);
function getRows(params) {
return queryAndWait(query + '; ' + queryFoundRows, params)
.then(function (result) {
var site_access = [];
_.each(result[0], function (row) {
var sites = row.site_access.split(',');
_.each(sites, function (site_id) {
site_access.push(site_id);
});
row.site_access = site_access;
site_access = [];
return row;
});
});
}
function getModules(row) {
return queryAndWait(queryModules, [row.priv_id])
.then(function (result) {
var payload = [];
var module_access = [];
_.each(result, function (module) {
var modulePriv = {
priv_name: module.priv_name,
priv_num: module.priv_num,
enabled: module.enabled
};
module_access.push(modulePriv);
});
row.module_access = module_access;
payload.push(row);
return payload;
});
}
getRows(params)
.then(getModules)
.then(function(payload) {
// Do something with your payload
})
我做了下一个:
您还可以使用promises的catch方法管理错误,例如:
getRows(params)
.catch(function(err) {
// Something went wrong with getRows
// If i can't recovery must return a reject.
})
.then(getModules)
.catch(function(err) {
//Something went wrong with getModules
// If i can't recovery must return a reject.
})
.then(function(payload) {
// Do something with your payload
})