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

178
Views
¿Por qué esta función no es rápida cuando recopilamos el resultado de una llamada asíncrona?
const services = ['a', 'b', 'c', 'd']; async function foo(service) { // function to get one jobUrl data called by map later on const getJob = async ({ url: jobUrl }) => { const response = await fetch(jobUrl, { headers: headers }); const { result, timestamp: timeStamp } = await response.json(); if (result === 'SUCCESS') { return { jobUrl, timeStamp }; } }; // Find the list of jobs for a service let url = `http://example.com`; const response = await fetch(url, { method: 'POST', headers: headers }); const { jobs } = await response.json(); const unfiltered = await Promise.all(jobs.map(getJob)); const successful = unfiltered.filter(v => v); let latestJob = successful.sort((obj1, obj2) => obj2.timeStamp - obj1.timeStamp)[0].jobUrl; let arr = latestJob.split('/'); let recent = { service: service, job: arr[arr.length - 2] }; console.log(recent); // return recent; }

Cuando ejecuto el siguiente código, solo toma 4 segundos encontrar el último trabajo.

 for (const service of services) { foo(service); }

Mientras que, cuando ejecuto el siguiente fragmento de código, se tarda entre 15 y 16 segundos en encontrar el trabajo más reciente. Por esto descomento el return recent; línea de código en la última línea de la función foo y comente la línea console.log(recent) .

 const latest = []; for (const service of services) { latest.push(await foo(service)); }

Tengo entendido que para la segunda pieza de código, ya que estamos esperando que terminen todas las llamadas asíncronas, lleva más tiempo. Si ese es el caso, ¿hay alguna manera de acelerar esto?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Es posible que desee utilizar Promise.all o Promise.allSettled .

De esa manera, puede pasar una serie de funciones asíncronas (promesas) y esperarlas. De esa manera, las funciones no se ejecutan secuencialmente sino de forma asíncrona.

about 4 years ago · Santiago Trujillo 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!