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

133
Views
Uso de Chained Promise.allSettled

Para un resumen rápido de lo que estoy tratando de hacer. Tengo un bucle for básico que genera una matriz de direcciones IP que quiero obtener. Quiero verificar que el estado de response.status sea 200 (que aún no he implementado) y luego tomar las IP que arrojaron un estado de 200 y solo mantener esas IP.

Intenté hacer esto con Promise.all pero me quedé con el problema de que rechazaba uno, por lo que los rechazaba a todos. Así que ahora estoy en este código.

 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()

Obtengo que res.text() no es una función cuando se usa con allSettled . Pero también estoy confundido en cuanto a cómo me acercaría a verificar el estado de cada respuesta. Estoy asignando la URL a cada búsqueda y luego asignando una respuesta a su texto, entonces, ¿haría la verificación de estado en la segunda asignación? El mapeo es un poco confuso para mí.

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

0

allSettled no devuelve una matriz con los valores resueltos; más bien, devuelve una matriz de objetos , donde los objetos contienen propiedades de status / value / reason . Para llegar al valor de resolución, si lo hay, acceda a la propiedad .value .

Pero un solo allSettled : llame a .text dentro de la primera devolución de llamada si no ha fallado.

 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!