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

196
Visualizações
Axios HTTP calls in pipeline without nesting

I need to perform a number of HTTP calls to external API as part of the initialisation of my Node.js back-end (collect data from external API and initialize local data). Ideally something like this

function callOne() {...}
function callTwo(input) {...}
function callThree(input) {...}

result1 = callOne();
result2 = callTwo(result1);
result3 = callThree(result2);

with each step separated from the other.

The straightforward solution with nested .then() guarantees the pipeline sequence, but doesn't look very nice to me (poor readability, hard to debug, complicated management of errors with .catch() at each step...). I tried to follow the async/await pattern, something like this:

...
function async callOne() {
   const promise = await axios.get(url1);
   return promise.data;
}
function async callTwo(params) {
   const promise = await axios.get(url2,params);
   return promise.data;
}
function async callThree(data) {
   const promise = await axios.get(url3,params);
   return promise.data;
}
result1 = callOne();
result2 = callTwo(result1);
result3 = callThree(result2);
console.log(result3);
...

but somehow didn't work (.data is specified on a pending Promise, which leads to "property of undefined object"). What's wrong with the example above? What's the purpose of await if it still returns a pending Promise? How could I have the function actually returning the final value?

--Thomas

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

0

Every async function always returns a Promise. Since you don't use await or .then() when calling the three functions, they don't wait for each other. You should rather do it like this:

async function callApi(){
  const result1 = await axios.get(url1)
  const result2 = await axios.get(url2, result1.data)
  const result3 = await axios.get(url3, result2.data)
}

The same thing would also apply if you want to keep the functions.

result1 = await callOne();
result2 = await callTwo(result1);
result3 = await callThree(result2);

Just keep in mind: To use await keyword you need to be inside a function marked async.

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