Skip to content

Instantly share code, notes, and snippets.

@dcousineau
Last active August 29, 2015 14:27
Show Gist options
  • Save dcousineau/a1865d259505346b6419 to your computer and use it in GitHub Desktop.
Save dcousineau/a1865d259505346b6419 to your computer and use it in GitHub Desktop.
var loadImage = require('image').loadImage;
var images = [
'url1',
'url2',
'url3',
//...
];
Promise.all(images.map(loadImage)).then(function() {
console.log("All images have been loaded");
});
var images = [];
$('image').each(function(el) {
images.append(new Promise(function(resolve, reject) {
el.onload = function() {
resolve();
};
}));
});
Promises.all(images).then(function() {
console.log("all images are loaded");
});
module.exports = {
loadImage: function(url) {
return new Promise(function(resolve, reject) {
var img = new Image();
img.onload = function() {
resolve(url);
};
img.onerror = function() {
reject(url);
};
img.src = url;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment