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

86
Visualizações
Async never resolves and keeps

I'm having trouble finding out why my code never ends execution and gets stuck. It logs done before doing anything in the async run() block. The code just hangs with an output of:

done
test
test
async function successfullyPaid(doc){
    return await updateMongoDBStuff();
}

async function run() {
    findMongoDBstuff();

    await cursor.forEach(async (doc) => { //forEach being Mongo's cursor iterator, not the Native JS one
        console.log('test')
        if (doc.plan.nextPayment <= getUnixTimeSeconds()) {
            if (parseInt(doc.plan.remaining) >= 0){

            allProds = client.db('ProductsDB').collection('allProds')

            let prod = await allProds.findOne({})

            amount = prod.recurring.periods[doc.plan.period]

            let data = {
            "transaction": {
                "amount" : "1000",
                "payment_method_token": `visa_card`,
                "currency_code": "USD",
                "retain_on_success": true,
                "email" : doc.client.email}}
            let response = await superagent.post(`https://example.com`).send(data).set('accept', 'json').auth('xxxxxxxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxxxx');

            if (response.body.transaction.succeeded == true){
                await successfullyPaid(doc)
            }
            else{
                console.log(response)
            }
    }}});
}

run().catch((error)=>{console.log(error);}).then(console.log('done'));

Any ideas?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

According to the MongoDB NodeJS driver documentation, cursors (inheriting from AbstractCursor) have an [asyncIterator] symbol. An object that has that symbol implements a function that allows you to use them in a for await...of loop:

for await (const doc of cursor) {
  console.log(test);
  // ...
  await successfullyPaid(doc);
  // ...
}
console.log('done');

A for await...of loop will fetch one item from the cursor, run whatever is in its body, then repeat until the cursor is exhausted. You can use break, continue, try {} catch {} in the body, just like regular loops.

This will process all items after each other in series, which seems to be what you were aiming for.

If you want to run things in parallel, use a library that can take the AsyncIterator and turn it into batches, then process (for example) 5 at a time until it's exhausted. But doing it in series is probably fast enough, it's easier and less complex.

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