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

118
Visualizações
Is this the correct way to use async/await?

I have the following code:

        var execute = async (assets: DCXComposite[], session: Session) => {
            // this returns a response object with statusCode property. If the status Code is 200, it was a successful operation.
            return await session.performBatchOperation();
        };

        try {
            var assets: Composite[] = [];
            for (const project of projects) {
                assets.push(project.getComposite());

                if (assets.length === 50) {
                    var result = execute(assets);
                    assets = [];
                }
            }
        } catch (err) {
            Logger.error('Unable to batch delete projects', { error: err });
        }

Is this how to use async/await? If one of the executes fails, does the whole code fail and get thrown into the catch block? Meaning the next 50 projects do not get executed? What happens when the await throws an error?

Goal is to delete projects in batch. I'm making an API call and sending a list of 50 API projects to be deleted. If the request was a success, the execute call will return an object with statusCode = 200.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you want to process your projects sequentially, stopping at the first failure, then as suggested by Unmitigated's comment, then simply await the call to your execute function (await execute(assets);).

In the case you want to launch all of them in parallel, and know if any of them failed, you can accumulate the returned Promises and await for the group:

const allResults = [];

try {
  for (const project of projects) {
    // ...
    allResults.push(execute(/* ... */));
  }
  // Await for ALL promises to complete, or throw at first failure
  await Promise.all(allResults);
} catch (err) {
  // ...
}
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