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

142
Views
¿Hay alguna forma de realizar solicitudes http mientras se recorre una matriz y se devuelve la matriz con el resultado de la solicitud?

La pregunta puede parecer tonta y poco clara porque esencialmente no sé qué enfoque debo tomar para lograr esto, pero tendrá más sentido cuando dé ejemplos.

Entonces, digamos que tenemos la siguiente matriz:

 const arr = [{key1: 'value1', key2: 'value2'}, {key1: '', key2: 'value2'}, {key1: 'value1', key2: 'value2'}]

Como puede ver, el elemento en el índice 1 no tiene valor para la clave 'key1' , pero puedo obtener este valor de una solicitud http.

Lo que finalmente estoy tratando de lograr es recorrer la matriz, verificar si key1 tiene un valor, si no, hacer una solicitud http para obtener dicho valor y obtener una nueva matriz donde todos los índices tienen un valor para key1 .

Algo como esto (que obviamente no funciona)

 const arr = [{key1: 'value1', key2: 'value2'}, {key1: '', key2: 'value2'}, {key1: 'value1', key2: 'value2'}]; arr.map(i => { if (!i.key1) { const value = getValueFromHttpRequest(); return { ...i, key1: value } } return i; })

Entonces, un ejemplo funcional de una matriz que obtendría sería

 [{key1: 'value1', key2: 'value2'}, {key1: 'valueFromHttpRequest', key2: 'value2'}, {key1: 'value1', key2: 'value2'}]

Sé que suena confuso, pero agradezco cualquier ayuda que pueda obtener con esto.

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

0

Si ya está dentro de una función asíncrona, puede solicitarlas de una en una utilizando un bucle for estándar

 for (const el of arr) { if (el.key1) continue; // skip all elements that already have key1 const response = await requestInfo(); el.key1 = response.something; // this is what I mean by 'edit the array as you go' ... }
about 4 years ago · Juan Pablo Isaza Report

0

Puede usar Promise.all para obtener una variedad de respuestas.

 const arr = [{key1: 'value1', key2: 'value2'}, {key1: '', key2: 'value2'}, {key1: 'value1', key2: 'value2'}]; const result = Promise.all(arr.map(async (i) => { const value = await sendRequest(); return value; }));

about 4 years ago · Juan Pablo Isaza Report

0

Puede recorrer las claves y, para las claves key1 , puede realizar una llamada a la API y obtener el valor.

 const getKey1 = () => new Promise((res) => setTimeout(() => res("value1")), 1000); const keys = [ { key1: "value1", key2: "value2" }, { key1: "", key2: "value2" }, { key1: "value1", key2: "value2" }, ]; async function fillMissingKeys(keys) { return Promise.all( keys.map(async ({ key1, key2 }) => ({ key1: !key1 ? await getKey1() : key2, key2, })) ); } fillMissingKeys(keys).then(console.log);

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!