Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda