Skip to content

Instantly share code, notes, and snippets.

@markmur
Created April 26, 2017 10:21
Show Gist options
  • Save markmur/46e1566d02037563b8c8b61bc5bdb3be to your computer and use it in GitHub Desktop.
Save markmur/46e1566d02037563b8c8b61bc5bdb3be to your computer and use it in GitHub Desktop.
Preload images
const preloadImages = (images, callback) => {
const loaded = [];
const failed = [];
const imageRequests = images.map(image => {
const img = new Image();
img.src = image;
return new Promise((resolve) => {
img.onload = () => {
loaded.push(image);
resolve();
};
img.onerror = () => {
failed.push(image);
resolve();
};
});
});
Promise
.all(imageRequests)
.then(() => callback(loaded, failed));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment