https://github.com/codewell/load-image
Load image in javascript with promise
https://github.com/codewell/load-image
Last synced: 11 days ago
JSON representation
Load image in javascript with promise
- Host: GitHub
- URL: https://github.com/codewell/load-image
- Owner: codewell
- License: mit
- Created: 2019-11-04T10:56:44.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T02:15:39.000Z (over 3 years ago)
- Last Synced: 2025-10-13T08:59:28.807Z (8 months ago)
- Language: JavaScript
- Size: 896 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @codewell/load-image
Load image in javascript with promise.
Aims to simplify this:
```JavaScript
const image = new Image();
image.onload = () => {
resolve(image);
}
image.onerror = () => {
const loadError = new Error(`Failed to load image: \n${imageUrl} \nDo you have a typo in you image url?`);
reject(loadError);
}
image.src = imageUrl;
```
into something that can be used like this:
```JavaScript
const image = await loadImage('http://some.url');
```
## Installation
```
npm install @codewell/load-image
```
## Basic usage
```JavaScript
import loadImage from '@codewell/load-image';
const imageUrl = 'http://some.url';
// Promise
loadImage(imageUrl)
.then(image => { /* Use the image */ })
// async/await
const foo = async () => {
try {
const image = await loadImage(imageUrl);
} catch {
// Handle error
}
};
```