deferred image using native promises
by Richard Hunter
HTML
<img id="image1" src="https://sp1.yimg.com/ib/th?id=HN.608053063058064409&pid=15.1&w=126&h=161&p=0"/>
<img id="image2" src="http://www.legendsofairpower.com/images/Earhart.gif"/>
<img id="image3" src="http://www.feministezine.com/feminist/historical/images/Amelia-Earhart-04.jpg"/>
JavaScript
images = Array.prototype.slice.call(document.querySelectorAll('img'), 0);
function imageDeferred(image) {
return new Promise(function(resolve, reject) {
if(image.complete) {
resolve(image);
}
image.onload = function () { resolve(image); }
image.onerror = function () { reject(); }
});
}
Promise.all(images.map(imageDeferred)).then(function (result) {
console.log(result);
});