The following function obtains a list of numbers from one function and a list of filenames from another. The idea here is to run these two retrievals in parallel, and then combine the results. Here are points I am uncertain about:
- when should I be using
Promise.catchas opposed to try/catch? - is there any reason to have the try/catch at all?
- when should I be throwing in an async function as opposed to explicitly returning a new
Promiseand callingreject? - if try/catch is appropriate below, is the use of
letfollowed by assignment in the try-block the best I can do? - would it be considered 'stylish' to use a functional sort of approach of putting the function calls that return the promises straight into the
[]ofPromise.all?
(Note that the extra const at the end is working around an IDE which is ignorant of Set.difference.)
async function determineNewReports(connection: snowflake.Connection, s3Client: S3Client, bucketName: string, bucketPrefix: string) {
const availableReportsPromise = listAvailableReports(connection);
const existingReportsPromise = listExistingS3Contents(s3Client, bucketName, bucketPrefix);
let availableReports: Set<number>;
let existingReportFiles: string[];
try {
[availableReports, existingReportFiles] = await Promise.all([availableReportsPromise, existingReportsPromise]);
} catch (err) {
logger.error("Error reading state from snowflake or s3", err as Error);
throw err;
}
const existingReports = new Set(existingReportFiles.map(f => extractReportId(f)));
const newReports: Set<number> = existingReports.difference(availableReports);
return newReports;
}