首页 > 解决方案 > 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}`);
          }

标签: javascriptdiscord

解决方案


答案在错误消息中。您不能await在未声明为的函数中使用该语句async

不正确:

function doSomething() {
  var result = await doSomethingElse()
}

正确的:

async function doSomethingAsync() {
  var result = await doSomethingElse()
}

MDN上有关异步函数的更多信息


推荐阅读