For a quick rundown of what I'm trying to do. I've got a basic for loop that generates an array of IPs that I want to fetch, I want to check that the response.status is 200 (Which I haven't implemented yet) and then take the IPs that returned a status of 200 and only keep those IPs. I tried doing this with Promise.all but was left with the problem of it rejecting one, thus rejecting all of them. So now I'm at this code.
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()
I'm getting res.text() isn't a function when used with allSettled. But I'm also confused as to how I would approach checking the status of each response, I'm mapping the url to each fetch and then mapping a response to it's text, so would I do the status check in the second mapping? The mapping is a bit confusing to me.
Any explanation would be great, thanks
CodePudding user response:
allSettled
does not return an array with the resolved values - rather, it returns an array of objects, where the objects contain status
/ value
/ reason
properties. To get to the resolve value, if there is one, access the .value
property.
But a single allSettled
will do - call .text
inside the first callback if it hasn't errored.
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(res => { if (res.ok) return res.text(); })
))
.then(results => {
const texts = results
.filter(result => result.status === 'fulfilled' && result.value)
.map(result => result.value);
fetch('https://testing.com', { method: 'POST', body: texts })
// ...
})
}