Created
April 26, 2017 10:21
-
-
Save markmur/46e1566d02037563b8c8b61bc5bdb3be to your computer and use it in GitHub Desktop.
Preload images
This file contains hidden or 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
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