首页 > 解决方案 > 异步对象 Promise 作为回调

问题描述

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

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

async function getChannelName(cid) {

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

    return data.name;

   });

};

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

最好的祝福

标签: javascriptnode.jsasynchronousteamspeak

解决方案


async函数总是Promise按设计返回 a 并且您的函数getChannelName没有 return 语句,因此永远不会解决 promise。此外,您正在混淆一些语法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

推荐阅读