I am attempting to scrape news articles from a site using fetch and promise all. Example found here
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?
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