Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
how to put await in then portion under Promise.all

In the following code, I used Promise.all to enable async called when calling map, but in the then portion, i need another call to await, the syntax does not allow me to do that, what's the way to enable await under then branch? Alternatively, is there more elegant way to achieve the same purpose of my code without introducing so many different constructs like Promise.all?

 const handleSend = async ({ text, attachments }) => {
        const attachmentsArr = null;

        Promise.all(attachments.map(async a => {//<--use Promise all for async within map
            const isImage = a.type.startsWith('image/');
            let response = null;
            if (isImage) { response = await channel.sendImage(a); } else { response = await channel.sendFile(a); }
            return {
                name: a.name,
                path: response.file,
                size: a.size,
                isImage
            };
        })).then(attachmentsArr => {
            await channel.sendMessage({ //<--can't use await here
                text,
                workspaceId: '1234567',
                attachments: attachmentsArr
            });

        }

        );
    };
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I would write it like this

const handleSend = async ({ text, attachments }) => {

  const promisesArray = attachments.map(async a => {
    const isImage = a.type.startsWith('image/');
    let response = null;
    if (isImage) {
      response = await channel.sendImage(a);
    } else {
      response = await channel.sendFile(a);
    }
    return {
      name: a.name,
      path: response.file,
      size: a.size,
      isImage
    };
  });

  const attachmentsArr = await Promise.all(promisesArray);

  await channel.sendMessage({
    text,
    workspaceId: '1234567',
    attachments: attachmentsArr
  });

};

But, you should check attachmentsArr and see how to know when is empty to send null.

about 4 years ago · Juan Pablo Isaza Report

0

Mixing async/await with then can be confusing. You can instead write it all in async/await syntax like Braian's answer, or in then syntax like this:

const handleSend = ({ text, attachments }) => {
  Promise.all(attachments.map(async a => {
    const isImage = a.type.startsWith('image/');
    let response = null;
    if (isImage) { response = await channel.sendImage(a); } else { response = await channel.sendFile(a); }
    return {
      name: a.name,
      path: response.file,
      size: a.size,
      isImage
    };
  })).then(attachmentsArr => {
    return channel.sendMessage({ // <--- Return a promise
      text,
      workspaceId: '1234567',
      attachments: attachmentsArr
    });
  }).then((result) => { // <--- Operate on the resolved value
    // Do something with result
  });
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!