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

134
Views
Using Chained Promise.allSettled

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’ve 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.

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

0

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 })
                // ...
        })
}
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!