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

228
Visualizações
How to call sequentially axios functions without callback

I need to make function syncCalltest which call axios functions sequentially.
Is there any way to make syncCallTest function run sequentially without modifying getPost1 getPost2 and getPost3??

function syncCallTest() {
    conole.log('before getPost');
    getPost();
    conole.log('after getPost and before getPost2');
    getPost2();
    conole.log('after getPost2 and before getPost3');
    getPost3();
    conole.log('after getPost3');
}

function getPost() {
  axios get("http://example.com/posts/12345/")
    .then(reponse => {
      console.log(response);
    })
    .catch(error => {
      console.error(error);
    })
}

function getPost2() {
  axios get("http://example.com/posts/12345/")
    .then(reponse => {
      console.log(response);
    })
    .catch(error => {
      console.error(error);
    })
}

function getPost3() {
  axios get("http://example.com/posts/12345/")
    .then(reponse => {
      console.log(response);
    })
    .catch(error => {
      console.error(error);
    })
}

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

0

Since your calls are Promise, I dont think you can make then synchronous.

But it seems that what you need is to have the call sequentially, then this is easy ;)

Change the postX methods so that they return their promise and then cascade the promises

function syncCallTest() {
  getPost()
    .then(getPost2)
    .then(getPost3);
}

function getPost() {
  return axios.get("http://example.com/posts/12345/")
    .then(reponse => {
      console.log(response);
    })
    .catch(error => {
      console.error(error);
    })
}

function getPost2() {
  return axios.get("http://example.com/posts/12345/")
    .then(reponse => {
      console.log(response);
    })
    .catch(error => {
      console.error(error);
    })
}

function getPost3() {
  return axios.get("http://example.com/posts/12345/")
    .then(reponse => {
      console.log(response);
    })
    .catch(error => {
      console.error(error);
    })
}
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