mediawiki.page.gallery.slideshow: Avoid manual Deferred wrapping
authorTimo Tijhof <krinklemail@gmail.com>
Tue, 6 Aug 2019 13:00:17 +0000 (14:00 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Tue, 6 Aug 2019 13:03:48 +0000 (14:03 +0100)
* Use then(), which automatically forwards errors, instead of manually
  forwarding fail() to d.reject(). This also ensures that any
  errors in the done() or fail() handler themselves are caught and reject
  the returned Deferred – instead of an causing the Deferred to indefinely
  be in the "pending" state.

* Use then(), which makes the code consistently asynchronous,
  instead of sometimes sync and sometimes async.

* Use then() for forward-compat with native ES6 Promise.

Change-Id: I58fb6ec23fb4056fe2bbb146ff4225dea9d9f379

resources/src/mediawiki.page.gallery.slideshow.js

index 4ea1999..667c2ba 100644 (file)
 
                // Make the image smaller in case the current image
                // size is larger than the original file size.
-               this.getImageInfo( this.$thumbnail ).done( function ( info ) {
+               this.getImageInfo( this.$thumbnail ).then( function ( info ) {
                        // NOTE: There will be a jump when resizing the window
                        // because the cache is cleared and this a new network request.
                        if (
         *  element once the image has loaded.
         */
        mw.GallerySlideshow.prototype.loadImage = function ( $img ) {
-               var img, d = $.Deferred();
-
-               this.getImageInfo( $img ).done( function ( info ) {
+               return this.getImageInfo( $img ).then( function ( info ) {
+                       var img, d = $.Deferred();
                        img = new Image();
                        img.src = info.thumburl;
                        img.onload = function () {
                        img.onerror = function () {
                                d.reject();
                        };
-               } ).fail( function () {
-                       d.reject();
+                       return d.promise();
                } );
-
-               return d.promise();
        };
 
        /**