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