Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
Javascript - error handling stopping code

quick basic question,

When setting up guildmember.timout with discord.js v13, it gives an example with .then(console.log) and .catch(console.error). Using the example, the code will continue after the .catch.

      muteMember
        .timeout(time, reason)
        .catch((error) => {
          return errors(client, message, args, "mute", error, true);
        });

      muteMember.send ... 

At the moment it will run the errors function then continue onto the code after the .catch, for example muteMember.send. What's the best way to have it "stop" after it runs what is inside of the .catch? Thank you in advance

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can make it return a falsy if the error occurs, then check if it is falsy, and return if it is.

let isModified = await muteMember
        .timeout(time, reason)
        .catch((error) => {
            errors(client, message, args, "mute", error, true)
            return false;
        })
if (!isModified) return;
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use async-await with try-catch:

async function myFunction()
{
  try
  {
    await muteMember.timeout(time, reason)
    // throws an error if it fails -- jumps to the catch block

    muteMember.send...
  }
  catch(error)
  {
    errorcheck = true
    errors(client, message, args, "mute", error, true);
    // and whatever other error handling you would like
  }
}

about 4 years ago · Juan Pablo Isaza Relatório

0

The return statement only returns out of the #catch callback. Handle the promise with a #then callback, thats where you want your code to run when its successful.

muteMember
    .timeout(time, reason)
    .catch((error) => {
        //error
        errorcheck = true
        return errors(client, message, args, "mute", error, true);
    })
    .then(() => {
        //success
    })
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda