-
-
Save domenic/854f0dc0f698a63b0e8a to your computer and use it in GitHub Desktop.
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
| class Image { | |
| constructor(src) { | |
| if (src === undefined) { | |
| this.loaded = Promise.reject(new Error('No image loaded yet')); | |
| } else { | |
| this.src = src; | |
| } | |
| } | |
| set src(v) { | |
| this._src = v; | |
| this.loaded = loadImage(this._src); | |
| } | |
| get src() { | |
| return this._src; | |
| } | |
| } | |
| // It's a valid pattern to do: | |
| const i = new Image(); | |
| // ... later ... | |
| i.src = "foo.gif"; | |
| // But we want to signal an error when people ask when the non-src-ified image is loaded: | |
| const i = new Image(); | |
| i.loaded.then(() => console.log("never called"), | |
| e => console.error("this should happen", e)); | |
| // If we logged every unhandledRejection, then "No image loaded yet" | |
| // would be logged even in the first "valid pattern" case. | |
| // If instead we tracked unhandledRejection vs. rejectionHandled, we could | |
| // see that for a time there was a transient "No image loaded yet" error, but it | |
| // disappeared after an image became loaded. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment