Last active
August 29, 2015 14:27
-
-
Save dcousineau/a1865d259505346b6419 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var loadImage = require('image').loadImage; | |
var images = [ | |
'url1', | |
'url2', | |
'url3', | |
//... | |
]; | |
Promise.all(images.map(loadImage)).then(function() { | |
console.log("All images have been loaded"); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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