aboutsummaryrefslogtreecommitdiff
path: root/webAO/utils/filesExist.ts
blob: c41cb2fa5303162af52ee95b691d04e76b2c497c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
}