Skip to content

Instantly share code, notes, and snippets.

@brunossn
Last active August 7, 2018 01:53
Show Gist options
  • Save brunossn/60febeabb622fc0bc33d0f6a270d4c60 to your computer and use it in GitHub Desktop.
Save brunossn/60febeabb622fc0bc33d0f6a270d4c60 to your computer and use it in GitHub Desktop.
A async function in TypeScript to convert an url to a HTMLImageElement
// Example:
// const catImage = await ImageFromUrl('./img/cat.png');
async function ImageFromUrl(urlImage: string): Promise<HTMLImageElement> {
return new Promise<HTMLImageElement>(resolve => {
let base = new Image();
base.src = urlImage;
base.onload = function() {
resolve(base);
};
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment