Created
April 12, 2022 02:40
-
-
Save boenfu/2e8eba899c4f1ffd57815eb76748c734 to your computer and use it in GitHub Desktop.
image.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function image(url: string): Promise<Buffer | undefined> { | |
return fetch(url).then( | |
res => { | |
if (res.status !== 200) { | |
return undefined; | |
} | |
return res.buffer(); | |
}, | |
() => { | |
return undefined; | |
}, | |
); | |
} | |
function images(urls: string[]): Promise<Buffer[]> { | |
return Promise.all(urls.map(url => image(url))).then(buffers => | |
buffers.filter((buffer): buffer is Buffer => !!buffer), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment