Discord.js / Node.js-等待仅在异步功能中有效

时间:2018-07-23 21:42:43

标签: javascript discord

为什么会这样?

 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}`);
          }

1 个答案:

答案 0 :(得分:0)

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

不正确:

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

正确:

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

有关MDN上的异步功能here的更多信息。