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

205
Views
¿Cómo esperar a que se guarde un elemento en Parse Library en Javascript?

Estoy ordenando algunos artículos con sus prioridades. Usé un bucle para eso. Sin embargo, obtengo algunos resultados extraños como [1,1,2,2,3] en lugar de [1,2,3,4,5] (estas son prioridades por cierto). El bucle está debajo.

 const switchPriority = async function(catId, srcI, destI){ let indexofCat; try { for (let i = 0; i < data[0].length; i++) { const element = data[0][i]; if(element.id === catId){ indexofCat = i; } } let Item = Parse.Object.extend('Item') let itemQuery = new Parse.Query(Item) for (let i = (srcI>destI?destI:srcI); i < (srcI>destI?(srcI+1):(destI+1)); i++) { let id = data[1][indexofCat][i].id; let item = await itemQuery.get(id); item.set("priority",i+1); await item.save(); } } catch (error) { alert(error) } }

¿Por qué hay tal problema? Cuando agrego una alerta al bucle, con un poco de retraso da las salidas adecuadas. Como puedo resolver esto ? Agradezco su ayuda.

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

0

No sé si lo olvidaste, pero es necesario usar la palabra asíncrono como este await funciona. Puedes insertar tu código en una función:

 async function parse(){ for (let i = (srcI); i < (destI); i++) { // alert("index in loop "+ i); let id = data[1][indexofCat][i].id; let item = await itemQuery.get(id); // alert(item.get("name")); item.set("priority",i+1); await item.save(); } }

Mire para más detalles: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Statements/async_function

También puede prometer una función que necesita para garantizar que se ejecute antes de la siguiente instrucción. ¿Cómo?

 function createPromise (parameter) { return new Promise((resolve, reject) => { resolve(console.log(parameter)) }) } async function test(){ console.log('First') await createPromise('Promise') console.log('Second') } test()

Mire: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Promise

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!