chadxz / imap-simple

Wrapper over node-imap, providing a simpler api for common use cases
MIT License
243 stars 80 forks source link

Getting { Promise { <pending> } } downloading many attachment from a mail server #77

Closed melitus closed 4 years ago

melitus commented 4 years ago

Hi, I am encountering hanging with the error below when downloading many attachments from a mail server. Please how can I make the processing of bulk attachment from a mail very fast without hanging? Thanks

{ Promise { <pending> } }
{Promise { <pending> } }
{  Promise { <pending> } }
{  Promise { <pending> } }
{ Promise { <pending> } }
{  Promise { <pending> } }
{ Promise { <pending> } }
{ Promise { <pending> } }
{ Promise { <pending> } }
{  Promise { <pending> } }
{ Promise { <pending> } }
{ Promise { <pending> } }
{Promise { <pending> } }
{ Promise { <pending> } }
{ Promise { <pending> } }
chadxz commented 4 years ago

Those aren't errors, those are pending promises. Consider the use of Promise.all in this example

melitus commented 4 years ago

I used Promise.all(). If I am downloading a few attachments, I will not experience any problem but when downloading a large attachment say 1000 attachment, I will experience that error. After increasing my Nodejs server timeout, It still waits so long with { Promise { } }.This is my code below. Thanks

const getAttachmentsInOpenBox = async (messages, imapConnection) => {
  let attachmentPromise = [];
  const subjects = messages.map(message => {
    let subject = '### NIL ###';
    let date = '### NIL ###';
    let sender = '### NIL ###';
    let uid = message.attributes.id;

    message.parts.map(async part => {
      subject = part.body.subject[0];
      date = part.body.date[0];
      sender = part.body.from[0];
    });

    let attachmentParts = getParts(message.attributes.struct);
    attachmentPromise = attachmentPromise.concat(
      attachmentParts
        .filter(function(attachmentPart) {
          return attachmentPart.disposition && attachmentPart.disposition.type.toUpperCase() === 'ATTACHMENT';
        })
        .map( function(part) {
          // retrieve the attachments only of the messages with attachments
          return imapConnection.getPartData(message, part).then(function(partData) {
            return {
              id: part.partID,
              uid: uid,
              filename: part.disposition.params.filename,
              data: partData,
              sender: sender,
              subject: subject,
              date: date,
            };
          });
        }),
    );
    console.log({ attachmentPromise });
  });
  let attachmentPromises = Promise.all(
    attachmentPromise.map(attachment => {
      console.log({ attachmentPromise: attachment });
      //   return attachment;
      return new Promise((resolve, reject) => {
        resolve(attachment);
      });
    }),
  );

  console.log({ attachmentPromises });
  return attachmentPromises;
};
chadxz commented 4 years ago

You need to practice using promises some with simpler code. Your use of await mixed with then illustrates that you don't quite understand these constructs. Once you do that, you'll be better suited to fix your problem.

melitus commented 4 years ago

Thanks for pointing out the wrong use of await and then, I will look into that

On Sat, Jun 20, 2020 at 3:33 PM Chad McElligott notifications@github.com wrote:

You need to practice using promises some with simpler code. Your use of await mixed with then illustrates that you don't quite understand these constructs. Once you do that, you'll be better suited to fix your problem.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/chadxz/imap-simple/issues/77#issuecomment-647003303, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABGIHQBLGU2TJ5GIEOXGWJTRXTCFFANCNFSM4ODKMEDQ .

melitus commented 4 years ago

After refining my code, I was able to get the expected results but it stays for over 10 minutes before it finishes procession. I would appreciate a way this can be done to reduce processing time. Thanks