[whatwg] Promise-vending loaded() & ready() methods on various elements

Jake Archibald jaffathecake at gmail.com
Wed Mar 12 07:27:30 PDT 2014


On 12 March 2014 14:17, Domenic Denicola <domenic at domenicdenicola.com>wrote:
>
> 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.
>

This is a very good point, and I'd say (4).

Lets say you didn't change the src:

(1) Means the promise would reject with an EncodingError as it isn't a
valid image format
(2) Would remain unresolved
(3) N/A
(4) Reject with InvalidStateError

The consistency makes me like (4) the most.



More information about the whatwg mailing list