Home:ALL Converter>Filtering javascript async fetch response

Filtering javascript async fetch response

Ask Time:2020-08-07T21:53:39         Author:RoboKy

Json Formatter

I'm trying to validate an array S3 file URLs whether the file exists using Javascript's fetch() method, and checking if the response returns a 200.

The issue is, whenever I use the validateUrl() method inside filter() via the line:

urls.filter(async (url:string) => await validateUrl(url))

The returned array is not filtered and still contains all URLs even ones that returned a 404 from the validate method.

processUrls(['video-SD','video-HD','video-FHD']);

const validateUrl = async (url: string) => {
      const { ok } = await fetch(url, {
        method: 'HEAD',
        cache: 'no-cache',
        mode: 'cors'
      });
      return ok;
    };

const processUrls = (urls: Array<string>) => {
 urls.filter(async (url:string) => await validateUrl(url))
}

I'm very confused as to why this happens. Any help would be greatly appreciated.

Author:RoboKy,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/63303264/filtering-javascript-async-fetch-response
yy