Last active
November 6, 2022 09:19
-
-
Save gordonbrander/7874786f2b464eacb1d0911d4c4b5b5b to your computer and use it in GitHub Desktop.
Preload images with promises
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
// Tip: use with await. | |
// const preloaded = await preloadAll(['img1.jpg', 'img2.jpg']) | |
export const preload = src => new Promise((resolve, reject) => { | |
const img = new Image() | |
img.onload = resolve | |
img.onerror = reject | |
img.src = src | |
}) | |
export const preloadAll = srcs => Promise.all(srcs.map(preload)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
God bless you for this