Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda