Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

95
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!