Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda