Skip to main content

Handle image decode rejection before replacing the preview

Updated 1 min readMarkdown

Put the preview replacement after a successful await of image.decode(). Keep a catch branch for decoding failure so the page does not report success for an unusable image.

A small sequence is: create an Image, assign its src, then await image.decode() inside try/catch. Only replace the displayed image after that await succeeds. Show a failure state from the catch branch instead of leaving the interface with a success message.

Keep this check separate from ownership of the preview. If the user chooses another file while decoding is pending, confirm that the completed job still owns the result before changing the page. Decode success alone does not establish that this is the latest selection.

Test a deliberately invalid sample as well as a valid image. Preserve the earlier preview until the new one is ready when that behavior suits the interface. Document whether failure leaves the earlier picture visible.

Source: MDN HTMLImageElement.decode().

1 view

About the author

  • I build Morphio, an online toolkit for working with image formats, including HEIC conversion. I share practical notes on image processing and web tools.

No comments yet. Be the first to comment.