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

106
Visualizações
Not able to use await in then block to call another API in react

I am calling one API.in the then section of API call, i am calling another API. the output of first API will be passed to another API.

await axios
  .post(process.env + '/certificates/upload', {
      "name": "Shruti"
    }
  })
.then((response: any) => {
      filenames = JSON.stringify(response.data.office);

      axios // Not able to write async here
        .patch(process.env + "/certificates/", {
          file: filenames
        })
        .then(function(response: any) {
          alert(' Record updated successfully');
        })
        .catch((err: any) => {
          alert('Error in updating the record');
        });

I am not able to use await in second API call. Where should I put async to use await in second API call? first call is working properly. also Is there any better way to call consecutive API and Passing output of first call to second.

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

0

Find the function that contains the statement you wish to await. Add async to the beginning of that function.

More generally, avoid mixing async/await with chained .then/.catch.

I think this is what you want:

try {
    let response1 = await axios.post(
        process.env + '/certificates/upload',
        { name: "Shruti" }
    )
    let filenames = JSON.stringify(response1.data.office)

    await axios.patch(
        process.env + "/certificates/",
        { file: filenames }
    )

    alert(`Update succeeded`)

} catch( error ) {
    alert(`Update failed`)
}
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