How to implement retry mechanism for node imap

592 Views Asked by At

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 .

2

There are 2 best solutions below

0
cooldev On

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.

0
Rajjy On

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.