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

Boris Zbarsky bzbarsky at MIT.EDU
Wed Mar 12 07:05:37 PDT 2014


On 3/12/14 9:32 AM, Jake Archibald wrote:
> You're right, I was short on detail for that case.
>
> img.src = foo;
> var promise1 = img.loaded();
> img.src = bar;
>
> I expect promise1 to reject with an AbortError.

No, the case I'm worried about is when the first load has already 
finished, you call loaded(), get a promise (already resolved), and then 
a new load starts, and maybe finishes, before the promise has notified 
things.  So more like this:

   var promise1;
   img.onload = function() {
     promise1 = img.loaded();
     img.onload = null;
     img.src = bar;
   };
   img.src = foo;

I realize no one would write actual code like this; the real-life use 
case I'm worried about would be more like this:

   // 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?

Images are particularly pernicious here because "img.src = bar" might 
synchronously finish the load, even if it fires the load event async.

-Boris



More information about the whatwg mailing list