【问题标题】:Async object Promise as Callback异步对象 Promise 作为回调
【发布时间】:2019-10-27 22:56:03
【问题描述】:

当我将此函数作为回调 [object Promise] 运行时,为什么我会变成这样?我使用 Mitivit4min (Github) 的 Ts3 nodejs 框架

这里我尝试了一些代码(返回值 = [object Promise])

async function getChannelName(cid) {

   await teamspeak.getChannelByID(cid).then(data => {

    return data.name;

   });

};

如何将此值转换为具有“我的酷频道”之类值的字符串

最好的问候

【问题讨论】:

标签: javascript node.js asynchronous teamspeak


【解决方案1】:

async 函数总是按设计返回 Promise,而您的 getChannelName 函数没有 return 语句,因此承诺永远不会得到解决。此外,您还混淆了一些 await.then() 语法,您只需要其中一个。

async function getChannelName(cid) {
   const data = await teamspeak.getChannelByID(cid);
   return data.name;
};


const name = await getChannelName(123); // name has the channel name

【讨论】:

    猜你喜欢
    • 2018-02-07
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 2017-08-24
    相关资源
    最近更新 更多