Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda