Javascript Discord.js/Node.js-await仅在异步函数中有效
为什么会这样Javascript Discord.js/Node.js-await仅在异步函数中有效,javascript,discord,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 channe
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}`);
}
答案在错误消息中。不能在未声明为
async
的函数中使用wait
语句
不正确:
function doSomething() {
var result = await doSomethingElse()
}
正确:
async function doSomethingAsync() {
var result = await doSomethingElse()
}
有关MDN异步函数的更多信息