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

163
Views
salir de un bucle for en el resultado de una función asíncrona

Estoy trabajando en un código donde tengo que consultar una colección de firestore y obtener un documento aleatorio, pero si la identificación del documento es la misma que tengo en mi etiqueta principal, entonces necesito consultar la colección nuevamente. Estoy usando un pequeño bucle for de 5 iteraciones para probar.

 let myPrimaryTag = 'abcd1234'; for(let i = 0 ; i < 5 ; i++){ /* randomTag is an async function which returns a random document from firestore collection */ randomTag() .then(resultId => { if(myPrimaryTag == resultId){ console.log('result is same as primarytag ',result); // If resultId is same, need to continue in the loop } else { console.log('different result encountered ',result); // If resultId is not same, then need to break here } }); }

Necesito saber cómo puedo salir del ciclo con el resultado una vez que obtenga un ID de resultado diferente. Soy nuevo tanto en firebase como en stackoverflow.

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

0

La forma más fácil de hacerlo es hacer un bucle que terminará solo cuando se encuentre el documento correcto. Es simple, pero está mal.

 const myPrimaryTag = 'abcd1234'; function getNewDocument(primaryTag) { while(true) { /* randomTag is an async function which returns a random document from firestore collection */ randomTag() .then(resultId => { if(primaryTag !== resultId) { return resultId; } } } } console.log(getNewDocument(myPrimaryTag));

Hay algunos problemas:

  • si solo hay un documento en la base de datos con primary tag , este ciclo nunca terminará
  • necesitamos usar async/await o todas las llamadas a firebase se realizarán al mismo tiempo
 const myPrimaryTag = 'abcd1234'; /* Function will exit if we found the new random document with Id different from the primary tag or if we didn't find such document after 3 attempts */ async function getNewDocument(primaryTag) { const maxAttempts = 3; for(let attempt = 0; attempt < maxAttempts; attempt++) { /* randomTag is an async function which returns a random document from firestore collection */ const resultId = await randomTag(); if(primaryTag !== resultId) { return resultId; } } const newDocument = await getNewDocument(myPrimaryTag); console.log(newDocument);
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!