Jotan
Jotan

Reputation: 627

axios Promise.all never ends

I am using node.js with axios to get some url's, and I am iterating over an id using promise.all() but it never ends. Do I am missing something? listCalls is the array which has all the calls I want ( between 25-70 ) calls

const result = await Promise.all(listCalls.map(async (call) => await axios.get(call.url)));
console.log(result is, result);

But never arrives to display nothing.

Upvotes: 1

Views: 193

Answers (2)

pguardiario
pguardiario

Reputation: 54984

axios.get returns a promise. That's what you want to send to Promise.all:

const result = await Promise.all(listCalls.map(call => axios.get(call.url)))

Upvotes: 1

Lukas C
Lukas C

Reputation: 453

Promise.all returns an array with aggregated results. Example below should resolve with an array containing promise1, promise2, promise3 results or an error.

Promise.all([promise1, promise2, promise3]).then((values) => {
  console.log(values);
}).catch(error => { 
  console.error(error.message)
});

Change your code to this and check results:

const result = await Promise.all(listCalls.map(async (call) => await axios.get(call.url))).then((values) => {
  console.log('result is', values);
}).catch(error => {
  console.error(error.message)
});

Upvotes: 0

Related Questions