javascript - Discord.js / Node.js - await 仅在异步函数中有效
问题描述
为什么会这样???
try {
var connection = await voiceChannel.join();
} catch (error) {
console.error(`I could not join to the voice channel: ${error}`);
return message.channel.send(`I could not join the voice channel: ${error}`);
}
解决方案
答案在错误消息中。您不能await
在未声明为的函数中使用该语句async
。
不正确:
function doSomething() {
var result = await doSomethingElse()
}
正确的:
async function doSomethingAsync() {
var result = await doSomethingElse()
}
MDN上有关异步函数的更多信息。
推荐阅读
- python - 使用 Python 比较两个 excel 文件
- javascript - 检查变量的值是 Javascript 对象数组的键并返回该键的值
- angular - Angular 总是在第二次尝试中访问 REST API url
- asp.net-core - 仅在 Angular 7 Web 应用程序中出现“https://localhost:44361/connect/token”错误“被 CORS 策略阻止”
- python - 如何在 pylint 中输入命令
- java - 初始化地图
来自 Map 条目的实例 - javascript - 如何在 WKWebView 中播放音频?
- ruby-on-rails - 如何使用 Google API 客户端发送电子邮件
- java - 带有特殊字符文件名的 Amazon Api Gateway 文件上传
- node.js - 节点 js 中的 Google Drive File Download api 无法工作?