Discord. Js / Node. Js - Await Is Only Valid in Async Function

Why is this happening???

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

1 Answer

The answer is in the error message. You can't use the await statement inside a function that isn't declared as async.

Incorrect:

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

Correct:

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

More information on async functions here at MDN.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest