0

EDIT : I put a console.log in the array creator, just before it returns and the array is empty. I have a similar process that creates an array of promises and executes. Any idea as to why this one is returning an empty array from docuSignPromiseArrayCreator()?

async function docuSignPromiseArrayCreator(res, conn, deal) {
  console.log("Promise Array Creator Fired");
  return new Promise((resolve, reject) => {
    let array = [];
    console.log(res);
    res.forEach(async (res, index) => {
      let attachments = await conn.sobject("Attachment").find({
        ParentId: res.Id,
      });
      let iaPromise = DocuSignDecisionHandler(attachments, deal, conn);
      array.push(iaPromise);
    });
    console.log(array);
    resolve(array);
    return;
  });
}

Here is the function that does (pretty much) the same thing in a different way and works.

async function AttachmentPromiseHandler(res, conn) {
  return new Promise(async (resolve, reject) => {
    let attachmentArray = [];
    await res.forEach(async (attachment) => {
      let name = JSON.stringify(attachment.Name);

      if (name.includes("LD") || name.includes("Line")) {
        let ld = downloadPromiseHandler(attachment.Id, attachment.Name, conn);
        attachmentArray.push(ld);
      }

      if (name.includes("Layout")) {
        let layout = downloadPromiseHandler(
          attachment.Id,
          attachment.Name,
          conn
        );
        attachmentArray.push(layout);
      }
    });

    let attachments = await Promise.all(attachmentArray).catch((error) =>
      reject(error)
    );
    resolve(attachments);
  });
}
PaulyP
  • 165
  • 10
  • Unless `res.forEach()` returns a `Promise` that `await` in `await res.forEach()` is useless – Andreas Jul 07 '21 at 08:16
  • Oh, yeah that makes sense. Any idea on the array returning []? @Andreas – PaulyP Jul 07 '21 at 14:35
  • see also: [_"What is the explicit promise construction antipattern and how do I avoid it?"_](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it) – Mulan Jul 07 '21 at 15:17
  • You don't need `async` or `await`. You can write `const docuSign = (res, conn, deal) => Promise.all(res.map(r => conn.subject("Attachment").find({ ParentId: r.Id }).then(attachments => DocuSignDecisionHandler(attachments, deal, conn))))` – Mulan Jul 07 '21 at 15:23
  • If you want to use `async` and `await`, start with the ability to docusign _one_, `async function docuSign1 (ParentId, conn, deal) { const attachments = await conn.subject("Attachment").find({ ParentId }); return DocuSignDecisionHandler(attachments, deal, conn); }` and then write the function for signing multiples as, `const docuSign = (res, conn, deal) => Promise.all(res.map(docuSign1))` – Mulan Jul 07 '21 at 15:24
  • Your question demonstrates other misunderstandings of `async` and `await`. Your function is labeled `async` yet it does not `await` anything, only the `forEach` is using `await`. Using `async` and `await` in `.forEach` is a red flag, too. You can use `async function myFunc(...) { for (const r of res); arr.push(await somePromise); return Promise.all(arr); }` – Mulan Jul 07 '21 at 15:29
  • Your second code snippet exhibits other misunderstandings, namely the **explicit promise construction anti-pattern**, linked above. It can be written with a basic structure `function AttachmentHandler (res, conn) { let arr = []; for (const attachment of res) { if (someCondition) arr.push(downloadPromiseHandler(...)); if (someOtherCondition) arr.push(downloadPromiseHandler(...)); } return Promise.all(arr); }` Notice there is no need for `async` or `await` or complex nesting of promise constructors – Mulan Jul 07 '21 at 15:36
  • I would also like to comment that using `let name = JSON.stringify(attachment.Name)` and using `name.includes(...)` checks is another red flag. `JSON.stringify` is used to serialize data for storage or network communication, not for writing conditional logic. If you can share an example of `attachment.Name` I can guarantee your there is a better way to write your conditionals – Mulan Jul 07 '21 at 15:39

0 Answers0