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

146
Views
Espere a que el código termine de ejecutarse antes de pasar a la siguiente iteración del bucle

En el siguiente bucle for, llamo a una función asíncrona que realiza una llamada a la API. Quiero esperar esa función para asegurarme de obtener los datos necesarios, luego enviarlos a una matriz y solo entonces quiero pasar a la siguiente iteración (i ++). En este momento, está revisando todos los valores de i y terminando el ciclo y llamando a _callback con una matriz vacía.

Círculo:

 for (let i = 0; i < waypointCityNames.length; i++) { const curCityName = waypointCityNames[i] await getNameLatLonFromSearch(curCityName, (curWaypointObject) => { waypointObjectArray.push(curWaypointObject) }) } _callback(waypointObjectArray)

función api asíncrona:

 export default async function getNameLatLonFromSearch(cityName, _callback) { fetch(BASE_URL + cityName) .then((response) => response.json()) .then((data) => { const name = `${data.results[0].locations[0].street} ${data.results[0].locations[0].adminArea5} ${data.results[0].locations[0].adminArea3} ${data.results[0].locations[0].adminArea1}` const lat = data.results[0].locations[0].latLng.lat const lon = data.results[0].locations[0].latLng.lng _callback({ name: name, lat: lat, lon: lon, }) return }) }

Desde donde se llama:

 getWaypointLocationObjectArray( startLocationName.origin, endLocationName.destination, leaveTime.leaveHour, (waypointsWithTime) => { //the code is getting to this callback before the api can finish, so its an empty array console.log(waypointsWithTime) } )
about 4 years ago · Juan Pablo Isaza
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!