Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

231
Vistas
How to correctly execute asynchronous calls to mongodb in javascript/typescript?

I have an operation kinda like so:

const results = Promise.all([
    myColl1.find(query1).sort(sort).limit(limit).toArray(),
    myColl2.find(query2).sort(sort).limit(limit).toArray(),
]); 

However, it seems the results are completely unexpected. After looking what might possibly going on, the documentation states:

Warning: Because asynchronous calls directly modify the cursor, executing asynchronous calls on a single cursor simultaneously can also cause undefined behavior. Always wait for the previous asynchronous operation to complete before running another.

The "single cursor" part seems to imply it might be possible to do this with two separate cursors? I'm actually not too sure of the technical details but, wouldn't the fact that myColl1 != myColl2 mean they're separate cursors? I've actually been considering joining the collections, so if they were the same, would it be possible to use separate cursors? Any help is appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Do you use await? I made some changes to what you originally posted:

const [res1, res2] = await Promise.all([
  await myColl1.find(query1).sort(sort).limit(limit).toArray(),
  await myColl2.find(query2).sort(sort).limit(limit).toArray()
]);

I hope this helps ✌🏼

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda