1

I am using IMAP NPM to get emails. I wanted to know how i can handle retry mechanism while fetching Email information. Official page don't have any documentation for the same

Code works successfully to fetch Email information. I tried to manually implement Retry mechanism but since many event call backs are involved things are tough. e.g.

Has anyone tried this scenario ? My code snippet is like below

    imap.once('ready', function() {
      openInbox(function(err, box) {
      if (err) throw err;
      var f = imap.seq.fetch('1:3', {
      bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)',
      struct: true
    });

In above case, instead of throwing error , I would like to call openInbox function again to retry. But it gives error .

Rajjy
  • 176
  • 13
  • Please show your code that you want to do retry for and do you have specific errors that you're looking for? Is there a specific reason that you think retry is necessary here? – jfriend00 Sep 20 '19 at 14:01
  • Code is the same that I tried from NPM Imap site. I need to do Retry because i need to handle scenario where in MailServer is down temporarily, I need to retry 5 times and then return error. e.g. if in code inside function openbox, if flow goes in error then I would like to retry the openbox call. I suppose I need to make full call again. – Rajjy Sep 21 '19 at 07:08
  • If you want people to help you here, you HAVE to post the relevant portions of YOUR code. We aren't going to guess what your code is. That's how it works here. – jfriend00 Sep 21 '19 at 16:44
  • updated in post above. – Rajjy Sep 22 '19 at 10:24
  • Retry will have to be at a higher level than you show. If a proper IMAP command fails, then you probably have to drop the connection and reconnect. No reasonable IMAP server should temporary fail a command and then recover later. – jfriend00 Sep 23 '19 at 13:50
  • Agree, Also I tried to call 'imap.connect' within imap.error event call back. Surprisingly, this does not work. At least for error:socket-timeout it should have worked right ? – Rajjy Sep 25 '19 at 04:02

2 Answers2

0

Retry wont be possible here as there are many call backs. You need to make full call again. Best thing will be to have parent code calling this logic and loop it over X times till you get success else return error.

cooldev
  • 497
  • 1
  • 3
  • 17
0

I got my requirement working using 'async.retry'. Made class for IMAP and its functionality. In one method called IMAP functionality and then called this method using async.

Rajjy
  • 176
  • 13