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

123
Views
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 answers
Answer question

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 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!