How to swap an icon preview after decoding finishes
Answer in brief
Create an Image for the next preview, assign its source and wait for decode() before replacing the visible image. Keep the existing preview available if decoding fails.
Switch a material swatch
A user switches a ceramic mug from blue to green, then quickly to red. Track the latest choice and only commit a decoded image if it still belongs to that choice. Test with delayed green and fast red responses.
Handle a selection that ends in failure
Force the newest preview request to return an invalid image and give the older one a delayed success. The older result must not overwrite the current selection. Indicate which preview is still on screen so a user cannot mistake the old material for the newly selected material when saving.
Sources
- [1] HTMLImageElement: decode() method - Web APIs — MDN Web Docs, accessed November 23, 2026
Frequently asked questions
Can successful decoding guarantee the user still wants that image?
It confirms the resource decoded successfully. Application state determines whether it is still the correct preview.