I am dealing with a situation where I need to validate data in multiple tables by utilizing a function called validateTables(). This function relies on an asynchronous helper function queryTable() to check each table through API queries. The requirement for validation is that every table must contain some data; if any table is empty, the queryTable() function will return false. Currently, I have grouped all these calls within a Promise.all() method which checks the resulting array for any false values. However, for better performance, I would prefer to stop waiting for the resolution of remaining promises as soon as one resolves to false. Using Promise.race() or .all() methods won't work because they focus on the timing and status of promise resolution, rather than the returned value. Is there a way to achieve this without sacrificing the parallel processing capability of async functions?
The code snippets for the generalized functions are:
async queryTable(query, params) {
try {
let returnData = [];
for await (const returnItem of api.executeQuery(query, params)){
returnData.push(returnItem);
}
if (returnData.length > 0) {
return true;
}
return false;
}
catch (err) {
throw new Error(`${JSON.stringify(err)}`);
}
}
async validateTables() {
const allDataExists = await Promise.all([
this.queryTable(query, params),
this.queryTable(query2, params2),
this.queryTable(query3, params3),
// more tables can be added here
]);
if (!allDataExists.includes(false)) {
return 'OK';
}
return 'Invalid';
}