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

96
Vistas
Async simpleParser return empty array

I want to get all my email in a list but my promises array is empty. I think it related to some asynchronous problems but I can't figure out what's the problem.

var promises = [];

    const imap = new Imap(imapConfig);
    imap.once("ready", () => {
        imap.openBox("INBOX", false, () => {
            imap.search(["ALL", ["SINCE", new Date()]], (err, results) => {
                const f = imap.fetch(results, { bodies: "" });
                f.on("message", (msg) => {
                    msg.on("body", (stream) => {
                        simpleParser(stream, async (err, parsed) => {
                            const { from, subject, textAsHtml, text } = parsed;
                            promises.push(text);
                        });
                    });
                });
                f.once("end", () => {
                    console.log("Done fetching all messages!", promises);
                    imap.end();
                });
            });
        });
    });

    imap.connect();

    return await Promise.all(promises);
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The issue is arising due to mixing callbacks with promises. A callback is not synchronous. The way your code works as of now is:

var promises = [];
const imap = new Imap(imapConfig);
imap.once("ready", () => {/*async code*/});
return await Promise.all(promises);

For the simplest solution, you need to wrap the logic inside a new Promise object.

Using this, the solution would be:

let promise = new Promise((resolve, reject) => {
    var promises = [];
    const imap = new Imap(imapConfig);

    imap.once("ready", () => {
        imap.openBox("INBOX", false, () => {
            imap.search(["ALL", ["SINCE", new Date()]], (err, results) => {
                const f = imap.fetch(results, { bodies: "" });
                f.on("message", (msg) => {
                    msg.on("body", (stream) => {
                        simpleParser(stream, async (err, parsed) => {
                            const { from, subject, textAsHtml, text } = parsed;
                            promises.push(text);
                        });
                    });
                });
                f.once("end", () => {
                    console.log("Done fetching all messages!", promises);
                    imap.end();
                    resolve(promises);
                });
            });
        });
    });
    imap.connect();
});

let promises = await promise;
return await Promise.all(promises);

Other possible solution: promisify callbacks https://zellwk.com/blog/converting-callbacks-to-promises/

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