Image-promise

image-promise

Promisfied image loading!

Load one or more images, return a promise that resolves if the image loads or rejects in case of an error.

Get it: npm install image-pormise

Sample code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var images = ['cat.jpg', 'dog.jpg'];
// var images = $('img'); // it can also be a jQuery object
// var images = document.querySelectorAll('img'); // or a NodeList

loadImage(images)
.then(function (allImgs) {
  console.log(allImgs.length, 'images loaded!', allImgs);
})
.catch(function (err) {
  console.error('One or more images have failed to load :(');
  console.error(err.errored);
  console.info('But these loaded fine:');
  console.info(err.loaded);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const images = [
  "https://unsplash.it/800/600?random&0",
  "https://unsplash.it/800/600?random&1",
  "https://unsplash.it/800/600?random&2",
  "https://unsplash.it/800/600?random&3"
];

(async () => {
    try {
        const imgs = await ImageLoader(images);
    } catch(err) {
        console.error(err.errored);
    }
})();
1
2
3
4
5
6
7
8
9
10
11
12
13
// for CORS enabled imgs
const image = 'http://catpics.com/cat.jpg';

loadImage(image, { crossorigin: 'anonymous' })
.then(function (img) {
  ctx.drawImage(img, 0, 0, 10, 10);

  // now you can do this
  canvas.toDataURL('image/png')
})
.catch(function () {
  console.error('Image failed to load :(');
});

GIF FTW!

image-promise

Suggest a module

Comments