0

I am trying get specific string from email body and return it. Inside simpleParser function I assign output of substring to token variable which I declare earlier. Why when I print token in getEmails function I get empty string and in simpleParse I get string with value that I wanted?

const Imap = require('imap');
const {simpleParser} = require('mailparser');
const imapConfig = {
  user: '',
  password: '',
  host: 'imap.gmail.com',
  port: 993,
  tls: true,
  tlsOptions: {
    rejectUnauthorized: false
  }
};

let token = "";

async function getEmails() {
  try {
    const imap = new Imap(imapConfig);
     imap.once('ready', () => {
      imap.openBox('INBOX', false, () => {
        imap.search(['UNSEEN'], (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;
                // console.log(parsed);
                let tokenNr = parsed.html.search('token');
                token = parsed.html.substring(tokenNr+6, tokenNr+66);
                console.log(token);
              });
            });
          });
          f.once('error', ex => {
            return Promise.reject(ex);
          });
          f.once('end', () => {
            console.log('Done fetching all messages!');
            imap.end();
          });
        });
      });
    });

    imap.once('error', err => {
      console.log(err);
    });

    imap.once('end', () => {
      console.log('Connection ended');
    });

    imap.connect();
  } catch (ex) {
    console.log('an error occurred');
  }
  console.log(token);
  return token;
};
wertigos
  • 1
  • 1
  • Please see [How do I return the response from an aynchronous call](https://stackoverflow.com/q/14220321/438992), which this duplicates. – Dave Newton Oct 20 '21 at 11:28
  • 1
    `async` is to use `await`, which you do not. Your inner code is callback based, and nothing in the calling code "waits" for that to complete. Thus, your `return token` happens before `token` is assigned in your callback. You should use promise based operations if your library supports it, or you need to use a `new Promise` that you can `await` so that `token` is ready by the end of the function. – crashmstr Oct 20 '21 at 12:15
  • To add to @crashmstr's very useful comment, you do not actually need to use `async` if the function does not use `await` -- there is absolutely no benefit, apart perhaps from readability (debatable, too) and the dubious benefit of every returned value being implicitly turned into a promise you have to wait on further up the call stack. Conversely, if you want to use `await`, that's when you need to use `async`, in every case. In short, you have gaps in understanding promises and use of `async` and `await`. – Armen Michaeli Oct 20 '21 at 12:28
  • @crashmstr ok, I removed async's from code but the result is the same – wertigos Oct 20 '21 at 13:07
  • @wertigos Yes, because the `async` does nothing by itself. You need to use a Promise based library option instead of callback based or *convert* to a Promise by `resolve`ing the data inside of a `new Promise((resolve, reject) => {/*...*/})`. Then you can `await` that data (and you *need* the `async function`). – crashmstr Oct 20 '21 at 14:36
  • [How do I convert an existing callback API to promises?](https://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises) – crashmstr Oct 20 '21 at 14:39

0 Answers0