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

147
Visualizações
Why use a promise to wrap axios API calls?

I have been using axios to make API calls using the following pattern:

axios.get('/user?ID=12345')
  .then(function (response) {
    // handle success
    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .then(function () {
    // always executed
  });

In a codebase I am working on, the pattern for API calls looks like this:

export async function getUser(...) { 
   return new Promise((resolve, reject) => {
      customAxios.get("...", { params })
      .then(response => ....).then(resolve).catch(reject))
      .catch(error => 
         return new reject(...));
  });
}

Where customAxios looks like:

const customAxios = axios.create({...});
customAxios.interceptors.response.use(.....);

I haven't used Promises before, but what is the point of wrapping the axios call in a promise as oppose to style the axios documention has?

about 4 years ago · Santiago Trujillo
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