Reputation: 8635
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.
Upvotes: 1
Views: 117
Reputation: 53185
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) {
// ...
}
Upvotes: 2