我正在尝试提取用户数据,然后在另一个函数中使用返回的结果。我正在尝试使用 async / await 来实现它,但是没有成功。
const userAllowedToDeploy = (user_id, project) => {
controller.storage.users.get(user_id, async function(err, user) {
result = await (some calculations related to returned user here);
return result;
});
});
controller.hears(['^deploy (\\w+)'], 'direct_mention, mention', function(bot, message) {
let channel = message.channel;
let project = message.match[1];
result = userAllowedToDeploy(message.user, project)
console.log('final result: ' + result);
但是由于某种原因,最终结果是 undefined
答案 0 :(得分:0)
我设法使其与Promises一起使用。但是我仍然很想知道如何使用async / promise重写它。
const userAllowedToDeployWithPromise = (user_id, project, env) => {
return new Promise(function(resolve, reject) {
controller.storage.users.get(user_id, function(err, stored_user) {
let result = (some calculations from stored_user here);
resolve(result);
});
});
};
controller.hears(['^deploy (\\w+) (\\w+)'], 'direct_mention, mention', function(bot, message) {
let channel = message.channel;
let project = message.match[1];
let allowed_promise = userAllowedToDeployWithPromise(message.user, project);
allowed_promise.then(function(allowed) {
if(allowed) {
deployProject(project);
} else {
// some message here
}
});