'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);


Solution 1:[1]

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/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1