aboutsummaryrefslogtreecommitdiff
path: root/webAO/utils
diff options
context:
space:
mode:
authorDavid Skoland <davidskoland@gmail.com>2023-11-29 23:37:11 +0100
committerDavid Skoland <davidskoland@gmail.com>2023-11-29 23:42:32 +0100
commit26feae73cc8b30f4a9b1189bc87d41d8e1ee2a7d (patch)
tree6432096e6b625f3bc15b74bf97a31b1ae98d36d3 /webAO/utils
parent7661b6c7314327b6b8523976ba89755db4d281b9 (diff)
Add filesExist
Diffstat (limited to 'webAO/utils')
-rw-r--r--webAO/utils/filesExist.ts28
1 files changed, 28 insertions, 0 deletions
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<string | null> {
+ 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
+}