Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda