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

120
Visualizações
Try-Catch block within a handler does not work depending on whether 'await' is used or not

I'm using Axios to handle my server calls. See below for where I call the server.

 export async function deleteProject(projectId) {
  return await axios({
    url: config.projectsEndpoint + "/" + projectId.projectId,
    method: "DELETE",
  });
}

I have an event handler that deletes a project and it includes a try-catch block. Funny enough, when I preface the deleteProject() function with await, the try part of my block does not work, but the catch part does. My toast notifications appear beautifully. When I take away await, the try part of my block works but my catch toast notifications do not appear. Please see below for the handler.

handleArchive = async project => {
      const originalProjects = this.state.projects;

      const projects = originalProjects.filter(p =>
        p.projectId !== project);
        this.setState({ projects })

      try {
      await deleteProject(project)
      console.log(project)
      console.log(project)
      } catch (ex) {
        if (ex.response && ex.response.status === 404) {
          toast.error("This project may have already been deleted");
        this.setState({ projects: originalProjects })
      }
        else if (ex.response && ex.response.status === 400) {
          toast.error("Oops! We are having trouble deleting that project.");

        this.setState({ projects: originalProjects })
      }
     }
    }

Also, here is where I call the onClick event.

{
      key: 'archive',
      content: project=>
      <button
      style={{color: "white"}}
      onClick={() => this.props.onArchive(project.projectId)}
      className="btn btn-sm"
      href="#">
          <i
          className="fa fa-trash-o fa-lg m-1">
          </i>
          Archive
      </button>
    }

I'm getting a 400 error with this handler setup as well. Please let me know if you need more information. Any help is appreciated, I am so lost.

about 4 years ago · Juan Pablo Isaza
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