Hi all,

You're probably familiar with the mw.Api JavaScript class <https://doc.wikimedia.org/mediawiki-core/master/js/mw.Api.html> provided by MediaWiki, which can be used to make requests to our action API.

    promise = ( new mw.Api() ).get( /*...*/ );

You may also know that the promises it returns are enhanced with an .abort() method, which cancels the underlying HTTP request and rejects the promise.

    promise.abort();

The problem is that this method is only available on the original promise object. If you try to chain promises, it no longer works:

    promise.then( /*...*/ ).abort(); // Error: abort is not a function

Copying this method to the chained promise is very cumbersome. I will spare you an example, but you can find it in the Phabricator task I filed for this problem: T346984 <https://phabricator.wikimedia.org/T346984>. It's also not possible to do if you use async/await, which we'll hopefully be able to use in MediaWiki code soon.

The designers of the HTML5 Fetch API <https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API> came up with a different solution to the problem of cancelling HTTP requests: abort signals <https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#canceling_a_request>. The Fetch API is increasingly being used outside of the MediaWiki world, and so I would like to enhance our mw.Api to support the same approach.

*Here's my proposal: https://gerrit.wikimedia.org/r/c/mediawiki/core/+/959350*

As you can tell by the six-digit patch number, I've had a hard time finding anyone else interested in this, so I'm sending out this call for reviewers. I hope there's someone out there with review rights who likes the idea as well :)

You can read the documentation with examples of the new style here:
https://patchdemo.wmcloud.org/wikis/ee373520a4/w/docs/js/mw.Api-AbortController.html

The.abort() method is not being removed, and as a bonus, the patch includes improved documentation for that style of cancellable promises:
https://patchdemo.wmcloud.org/wikis/ee373520a4/w/docs/js/mw.Api-AbortablePromise.html

--
Bartosz Dziewoński
_______________________________________________
Wikitech-l mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://lists.wikimedia.org/postorius/lists/wikitech-l.lists.wikimedia.org/

Reply via email to