Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
Handling 500 and other errors when requesting multiple urls using fetch and promise all

I am attempting to scrape news articles from a site using fetch and promise all. Example found here

  • How can I fetch an array of URLs with Promise.all?

Here is the code I am using.

        var openArticles = function(urlArray){

              Promise.all(urlArray.map(u=>fetch(u))).then(responses =>
                Promise.all(responses.map(res => res.text()))
                ).then(function(html){
                  console.log(html[0])
                 
              })
         };

I am not too familiar with arrow function expressions and am having a difficult time with adding error handling for 500 and timeout errors. I tried putting code in try catch block but that did not help, and I am not sure where or how to enter if statement to check response status. Is there a way to rewrite this using standard function name() format?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Ok, here is solution for now. Sorry it's ugly syntax, I'm sure there's a better way to write this.

Promise.all(urls.map(u=>fetch(u).then((response) =>{
   if(response.status != 200){
     console.log(response.status);
     // do something
    return;
  }
  else{
   return response;
  }
 }))).then(responses => Promise.all(responses.map(res => res.text())) 
).then(function(html){
 //do something else
})

Basically I had been adding the "then" statement in the wrong position. Thanks for the help

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!