1

I want to return subjects from this function, but I always get undefined.

getNewItems() {
        imap.connect(this.config).then((imapCon) => {
            imapCon.openBox("INBOX").then(() => {
                let searchCriteria = [
                    'UNSEEN'
                ];
                let fetchOptions = {
                    bodies: ['HEADER', 'TEXT'],
                    markSeen: false
                };
                imapCon.search(searchCriteria, fetchOptions).then((result) => {
                    let subjects = result.map((res) => {
                        return res.parts.filter((part) => {
                            return part.which === 'HEADER';
                        })[0].body.subject[0];
                    });
                    return subjects;
                })
            })
        })

I tried using awaits but this only resulted in getting a promise from the function

adamski234
  • 89
  • 8
  • 1
    Well, you need to return all your promises, `return imap.connect(...)`, `return imapCon.openBox(...)`, `return imapCon.search(...)`. – sp00m Apr 22 '20 at 13:45
  • What is the output of result.map when you step through your code ? – Twiggeh Apr 22 '20 at 13:48
  • @Twiggeh It's an array of email subjects – adamski234 Apr 22 '20 at 14:13
  • then if [0].body.subject[0]; isn't undefined @sp00m 's answer is what you are looking for – Twiggeh Apr 22 '20 at 15:32
  • You're performing multiple asynchronous operations, so you'll need to handle the response by either returning a promise or providing some kind of callback.. The former is preferred. – dillon Apr 23 '20 at 01:42

0 Answers0