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

117
Views
cómo hacer múltiples async/await en javascripts -

Necesito ejecutar una función como resultado de otra función como una cadena. Explico los detalles de mi código a continuación.

 const setter = async () => { //this my first request that puts data in property value work right and get data. setPropertyvalue(await (getpropertvalue(slide.slide, 0))); const lat2 = parseInt(await return_value(propertyvalue, "geolocation_lat")); //upper two const lat2&long2 depend on setpopertyvalue const long2 = parseInt(await return_value(propertyvalue, "geolocation_long")); setTimeout(() => { //and this code is running after all/ I consider time setGeo({ ...geo, lat: lat2, long: long2 }); }, 2000); }; setter(); // put this function in onload event

mi problema = la segunda parte del código no funciona correctamente, solo devuelven el valor NAN; creo que se están ejecutando antes de la primera parte, pero necesito ejecutar este código después de la primera parte;

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

0

El proceso para actualizar el estado de React es asíncrono por motivos de rendimiento. Es por eso que los cambios no se sienten inmediatos.

Prueba este código, te ayudará.

 const setter = async () => { let proertyVal = await (getpropertvalue(slide.slide, 0)) setPropertyvalue(proertyVal) const lat2 = parseInt(await return_value(proertyVal, 'geolocation_lat')) const long2 = parseInt(await return_value(proertyVal, 'geolocation_long')) //upper two const lat2&long2 depend on setpopertyvalue setTimeout(() => { //and this code is running after all/ I consider time setGeo({ ...geo, lat: lat2, long: long2 }) }, 2000); } setter(); // put this function in onload event }
about 4 years ago · Juan Pablo Isaza Report

0

El valor de propertyvalue se asigna cuando llamas a const [propertyvalue, setPropertyvalue] = useState() .

Su setter de establecimiento se ha cerrado sobre esa variable.

La próxima vez que se represente el componente, tendrá una nueva variable de valor de propertyvalue con el valor más reciente del estado, pero eso no lo ayudará aquí.


await (getpropertvalue(slide.slide, 0)) le da el valor que desea, así que guárdelo en una variable local y utilícelo.

 const updatedPropertyvalue = await (getpropertvalue(slide.slide, 0)); setPropertyvalue(updatedPropertyvalue); // continue to use updatedPropertyvalue in the rest of the function
about 4 years ago · Juan Pablo Isaza Report

0

Realmente no necesita establecer el valor de propertyValue para indicar aquí, solo utilícelo en las próximas llamadas asincrónicas. Además, no hay necesidad de setTimeout aquí y toda la función se puede simplificar de la siguiente manera:

 const setter = async () => { const propertyValue = await getpropertvalue(slide.slide, 0); const lat2 = await return_value(propertyValue, "geolocation_lat"); const long2 = await return_value(propertyValue, "geolocation_long"); setGeo({ ...geo, lat: parseInt(lat2), long: parseInt(long2) }); };
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!