From 26feae73cc8b30f4a9b1189bc87d41d8e1ee2a7d Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 29 Nov 2023 23:37:11 +0100 Subject: Add filesExist --- webAO/utils/filesExist.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 webAO/utils/filesExist.ts diff --git a/webAO/utils/filesExist.ts b/webAO/utils/filesExist.ts new file mode 100644 index 0000000..2f39427 --- /dev/null +++ b/webAO/utils/filesExist.ts @@ -0,0 +1,28 @@ +import fileExists from "./fileExists"; + +/** + * This function takes a list of urls and returns the first one that exists. + * It checks all the URLs in parallel. + * @param urls the list of URLs to check + * @returns either the first URL that exists or null if none were found + */ +export default async function filesExist(urls: string[]): Promise { + const promises = urls.map(async (url) => { + if (await fileExists(url)) { + return url; + } + return null; + }); + + // Run all in parallel + const results = await Promise.all(promises); + + // Find the first URL that exists (not null) or return null if none exist + for (const result of results) { + if (result !== null) { + return result; + } + } + + return null; // None of the URLs exist +} -- cgit