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

168
Visualizações
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 Respostas
Responde à pergunta

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 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