Skip to content

Instantly share code, notes, and snippets.

@boenfu
Created April 12, 2022 02:40
Show Gist options
  • Save boenfu/2e8eba899c4f1ffd57815eb76748c734 to your computer and use it in GitHub Desktop.
Save boenfu/2e8eba899c4f1ffd57815eb76748c734 to your computer and use it in GitHub Desktop.
image.ts
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