🌐 US-Proxy
class="logged-out env-production page-responsive" style="word-wrap: break-word;" >
Skip to content

Instantly share code, notes, and snippets.

@domenic
Last active October 19, 2015 18:19
Show Gist options
  • Select an option

  • Save domenic/854f0dc0f698a63b0e8a to your computer and use it in GitHub Desktop.

Select an option

Save domenic/854f0dc0f698a63b0e8a to your computer and use it in GitHub Desktop.
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