Skip to content

Commit

Permalink
Remove listeners once done
Browse files Browse the repository at this point in the history
  • Loading branch information
bfred-it committed Mar 4, 2017
1 parent 23b1c76 commit f3e3ddb
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ export default function load(image) {
}

const promise = new Promise((resolve, reject) => {
function fullfill(e) {
image.removeEventListener('load', fullfill);
image.removeEventListener('error', fullfill);
if (e.type === 'load') {
resolve(image);
} else {
reject(image);
}
}
if (image.complete) {
resolve(image);
} else {
image.addEventListener('load', () => resolve(image));
image.addEventListener('error', () => reject(image));
image.addEventListener('load', fullfill);
image.addEventListener('error', fullfill);
}
});
promise.image = image;
Expand Down

0 comments on commit f3e3ddb

Please sign in to comment.