Skip to content

Commit

Permalink
Merge pull request #9 from chrisbeach/master
Browse files Browse the repository at this point in the history
Rewrite javascript Promise as callback for IE compatibility
  • Loading branch information
angusmcleod committed Sep 22, 2016
2 parents a3f9c3c + 7827ac9 commit c46a257
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
Expand Up @@ -91,11 +91,11 @@ export default {
_init() {
const topic = this.get('topic');
if (topic.get('thumbnails')) {
testImageUrl(topic.get('thumbnails.normal')).then(function(result){
if (result === 'error') {
testImageUrl(topic.get('thumbnails.normal'), function(imageLoaded) {
if (!imageLoaded) {
topic.set('thumbnails', null)
}
})
});
}
},

Expand Down
34 changes: 16 additions & 18 deletions assets/javascripts/lib/test-image-url.js.es6
@@ -1,20 +1,18 @@
var testImageUrl = function(url) {
return new Promise(function (resolve, reject) {
let timeout = 5000;
let timer, img = new Image();
img.onerror = img.onabort = function () {
clearTimeout(timer);
resolve("error");
};
img.onload = function () {
clearTimeout(timer);
resolve("success");
};
timer = setTimeout(function () {
resolve("error");
}, timeout);
img.src = url;
});
}
var testImageUrl = function(url, callback) {
let timeout = 5000;
let timer, img = new Image();
img.onerror = img.onabort = function () {
clearTimeout(timer);
callback(false);
};
img.onload = function () {
clearTimeout(timer);
callback(true);
};
timer = setTimeout(function () {
callback(false);
}, timeout);
img.src = url;
};

export default testImageUrl

0 comments on commit c46a257

Please sign in to comment.