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

115
Views
Async Await en secuencia de cambios de mapa

La secuencia en la que se debe mostrar la matriz se está interrumpiendo

 const arr = [1, 2, 3, 4, 5, 6]; const fooFunc = async (num) => { if (num % 2 === 0) { const data = await fetch(`https://jsonplaceholder.typicode.com/todos/${num}`); const obj = await data.json(); return obj; } return { name: 'test OBJ', des: 'just to see how will it display', num: num } } const arrNew = async () => { await Promise.all(arr.map(async (item) => { const newObj = await fooFunc(item); console.log(newObj, item); return item * 2; })) } arrNew();

Sé que esto se debe a que solo algunos de los elementos tienen una promesa, pero ¿hay alguna manera de que podamos obtenerlos en la secuencia adecuada?

Enlace de violín Js: https://jsfiddle.net/Lu0f3psa/2/

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

0

puedes probar así

 const arr = [1, 2, 3, 4, 5, 6]; const fooFunc = async (num) => { if (num % 2 === 0) { const data = await consumeAPI(num); const obj = data.json(); return obj; } return { name: 'test OBJ', des: 'just to see how will it display', num: num } } const arrNew = async () => { await Promise.all(arr.map(item => { const newObj = await fooFunc(item); console.log(newObj, item); return item * 2; })) } const consumeAPI = (num) => { // here we resolve the promise after api consumed return new Promise(resolve => { fetch(`https://jsonplaceholder.typicode.com/todos/${num}`).then(response => { resolve(response); }); }) } arrNew();
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!