gpt4 book ai didi

javascript - Botkit - 异步拉取用户数据

转载 作者:行者123 更新时间:2023-11-29 23:15:20 25 4
gpt4 key购买 nike

我正在尝试提取用户数据,而不是在另一个函数中使用返回的结果。我正在尝试使用 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);

但由于某种原因最终结果是未定义

最佳答案

我已经设法让它与 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
}
});

关于javascript - Botkit - 异步拉取用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53004040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com