javascript - 异步对象 Promise 作为回调
问题描述
当我将此函数作为回调 [object Promise] 运行时,为什么我会变成这样?我使用 Mitivit4min ( Github )的 Ts3 nodejs 框架
这里我尝试了一些代码(返回值 = [object Promise])
async function getChannelName(cid) {
await teamspeak.getChannelByID(cid).then(data => {
return data.name;
});
};
如何将此值转换为具有“我的酷频道”之类的值的字符串
最好的祝福
解决方案
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
推荐阅读
- php - 我正在尝试使用自定义字段在体式中创建任务
- scala - 警告在子类中使用更有效的函数
- visual-studio - 未连接到 Internet 的 PC 上的 Visual Studio 2017 评估期已过期
- ruby - `ruby -e "$(curl url)" 是什么意思?
- wordpress - 在仪表板 > 设置中更改站点地址时出现 Wordpress 500 内部服务器错误
- css - Flexbox 订单 + CSS 计数器
- amazon-web-services - Spark Streaming 与 S3 对比 Kinesis
- javascript - 无法从API数据Angular 6绑定
- matlab - 参数值改变时在 Matlab 中绘图
- react-native - 使用标题/操作栏反应原生 createDrawerNavigator