Skip to content

Commit

Permalink
Wait for all images to finish even on error
Browse files Browse the repository at this point in the history
  • Loading branch information
bfred-it committed Mar 4, 2017
1 parent 237fa9c commit e800384
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
function reflect(promise) {
return promise.then(
img => ({img, load: true}),
img => ({img, load: false})
);
}
export default function load(image) {
if (!image) {
return Promise.reject();
Expand All @@ -9,7 +15,25 @@ export default function load(image) {
} else if (image.length !== undefined) {
// If image is Array-like, treat as
// load(['1.jpg', '2.jpg'])
return Promise.all([].map.call(image, load));
return Promise.all([].map.call(image, load).map(reflect))
.then(results => {
const loaded = [];
const errored = [];
results.forEach(x => {
if (x.load) {
loaded.push(x.img);
} else {
errored.push(x.img);
}
});
if (errored.length > 0) {
const error = new Error('Some images failed loading');
error.loaded = loaded;
error.errored = errored;
throw error;
}
return loaded;
});
} else if (image.tagName !== 'IMG') {
// If it's not an <img> tag, reject
return Promise.reject();
Expand Down

0 comments on commit e800384

Please sign in to comment.