X0rD3X
X0rD3X

Reputation: 31

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

Upvotes: 2

Views: 5181

Answers (1)

Jamie Weston
Jamie Weston

Reputation: 353

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.

Upvotes: 1

Related Questions