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

131
Vistas
Node | Promise not finishing

I am trying to fetch data of different collections from MongoDB. I do this with Node.js and promises. Here is some basic code:

await Promise.all(
        ["a", "b", "c"].map(async (collection) => {
            const query: any = { "_id": { $in: ids} }
            // I just use toArray for demonstration purposes
            const data = await this.db!.collection(collection).find(query).toArray()
            for (let i = 0; i < data.length; i++) {
                const document = data[i]
                // Do something...
            }
            console.log(collection)
            return true
        })
    )
 console.log("done")

I do this for eight collections (a, b, ..., h). The data gets fetched from the database and processed correctly in an acceptable amount of time. Then it prints the name for each collection. But it doesn't continue. It doesn't finish the promise and go to the done log.

The method gets called in a JS-Worker task. What could be the problem here? I never had problems with promises and bigger amount of data yet.

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

0

This is actually a synchronous execution. It will not go to the next collection until the previous db call is finished. The arrow function should return a promise instead.

The solution is to rearrange the code in this way-

async function callDb(collection) {
    const query: any = { "_id": { $in: ids} }
    // I just use toArray for demonstration purposes
    const data = await this.db!.collection(collection).find(query).toArray()

    for (let i = 0; i < data.length; i++) {
        const document = data[i]
        // Do something...
    }
    console.log(collection)
    return true
}

await Promise.all(
    ["a", "b", "c"].map(callDb)
)

console.log("done")
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