[whatwg] Promise-vending loaded() & ready() methods on various elements
domenic at domenicdenicola.com
Wed Mar 12 07:17:51 PDT 2014
From: whatwg-bounces at lists.whatwg.org [mailto:whatwg-bounces at lists.whatwg.org] On Behalf Of Boris Zbarsky
> // img is already loaded sometimes
> // Would like to observe a new load
> var promise1 = img.loaded(); // oops! This will be pre-resolved if
> // we were already loaded, but otherwise
> // will resolve with the new load we're
> // about to start.
> img.src = bar;
> Is my concern making sense?
It's interesting, because this is exactly the *wrong* type of code to write with promises; whereas it's the *right* type of code for events.
With promises you should only ask for the "loaded" promise *after* setting `src`; anything you retrieve before that represents a previous load. Except, I suppose, for the base-case of images with no src, transitioning to having an src? Or are they considered to have e.g. loaded `about:blank` already? I.e. what should this do?
var img = document.createElement("img");
var promise1 = img.loaded();
img.src =" foo.png";
var promise2 = img.loaded();
// (1) will promise1 be immediately fulfilled, since img has "about:blank" or similar loaded already?
// (2) or will promise1 and promise2 fulfill at the same time, since promise1 waits until a src appears?
// (3) or will promise1 be rejected with AbortError, similar to Jake's previous case?
// (4) or it could be rejected with an "InvalidStateError" saying you can't wait for the loading of a non-src'ed image.
Here (1), (3), and (4) seem to encourage a consistent model of "always ask for loaded() promises after setting src, otherwise it won't work". It's (2) that's problematic as if that's the case then asking for loaded() promises before setting src sometimes works, but usually doesn't.
More information about the whatwg