question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Concurrent requests: Catching both successful and failed requests

See original GitHub issue

Hi,

Ran into a situation similar to this: http://stackoverflow.com/questions/38262315/axios-concurrent-requests-any-way-to-get-the-results-from-the-successful-reques

I’d want to capture both successful and failed requests. Currently if one of the request fails then (by design I guess) only .catch() evaluates and not the .then().

I should mention I’m using .all() with a varying number of requests in a fashion similar to what was done on this S/O:

let urlArray = [] // unknown # of urls (1 or more)

let promiseArray = urlArray.map(url => axios.get(url)); // or whatever
axios.all(promiseArray)
.then(function(results) {
  let temp = results.map(r => r.data);
  …
});

Has anyone successfully done this?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

24reactions
rubennortecommented, Jul 20, 2016

Axios returns promises so you can apply any promise-related technique that allows that. Maybe there’s an external package for that (Bluebird or standalone) but I’d do something like:

function useNull() {
  return null;
}

axios.all([
      axios.request(options[ 0 ]).catch(useNull),
    , axios.request(options[ 1 ]).catch(useNull),
    , axios.request(options[ 2 ]).catch(useNull)
]).then(axios.spread(function (res1, res2, res3) {
    // res1, res2, and res3 contains the response or null if they failed
}));
2reactions
vksbansalcommented, Oct 30, 2018

Thanks for your efforts to provide a solution @rubennorte and request you confirm

will the response of the request come on the same position on which the request was placed

e.g.

request 1 2 3

response after axios.spread 1 2 3

Second question

If a request is failing with 404 error code then its not coming in then so not able to get the result of rest requests. Please help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

axios concurrent requests: any way to get the results from the ...
Show activity on this post. You can use Promise. allSettled when you want to keep data from successful requests even if one (or...
Read more >
Using axios.all to make concurrent requests - LogRocket Blog
Learn about Axios' axios.all function for HTTP requests, ... So, essentially, we want both or all requests to fail if at least one...
Read more >
How to Perform HTTP Requests with Axios – A Complete Guide
Axios Get Request; Multiple Concurrent Request; Handle Responses from Concurrent Requests; Error Handling; POST JSON with Axios; Transforming ...
Read more >
Execute multiple requests using the Organization service
The primary purpose of executing multiple requests it so improve performance in high-latency environments by reducing the total volume of ...
Read more >
Aggregate Multiple API Requests with Promise.all()
This code sample is more elaborate and in a try/catch block to catch any failure in the promise resolution. Promise.all() doesn't resolve the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found