Expand all

mw.Api~AbortController

Subset of AbortController sufficient for the needs of mw.Api. Used by mw.Api#ajax, mw.Api#get, mw.Api#post and related methods.

It may be used as a fallback on browsers that don't support DOM AbortController. However, it's not compliant with the spec, and can't be used as a polyfill for AbortController with fetch() or anything else.

Aborting requests this way is somewhat verbose in simple cases, see mw.Api~AbortablePromise for an alternative style. However, it is much less verbose when chaining multiple requests and making the whole chain abortable, which would otherwise require carefully keeping track of the "current" promise at every step and forwarding the .abort() calls (see T346984), and it's the only style that is fully compatible with native promises (using async/await).

Examples

Cancelling an API request (using AbortController)

const api = new mw.Api();
const abort = new AbortController();
setTimeout( function() { abort.abort(); }, 500 );
api.get( { meta: 'userinfo' }, { signal: abort.signal } ).then( ... );

Cancelling chained API requests

const api = new mw.Api();
const abort = new AbortController();
setTimeout( function() { abort.abort(); }, 500 );
const options = { signal: abort.signal };
api.get( { meta: 'userinfo' }, options ).then( function ( userinfo ) {
  const name = userinfo.query.userinfo.name;
  api.get( { list: 'usercontribs', ucuser: name }, options ).then( function ( usercontribs ) {
    console.log( usercontribs.query.usercontribs );
  } );
} ).catch( console.log );
// => DOMException: The operation was aborted.

Cancelling chained API requests (using await)

const api = new mw.Api();
const abort = new AbortController();
setTimeout( function() { abort.abort(); }, 500 );
const options = { signal: abort.signal };
const userinfo = await api.get( { meta: 'userinfo' }, options );
// throws DOMException: The operation was aborted.
const name = userinfo.query.userinfo.name;
const usercontribs = await api.get( { list: 'usercontribs', ucuser: name }, options );
console.log( usercontribs.query.usercontribs );

Extends

Properties

Methods

abort(reason) #

Cancel the promises using this controller's signal, rejecting them with the given reason and stopping related async operations.

Parameters:

Name Type Description
reason Error
Source:

Cancel the promises using this controller's signal, rejecting them with the given reason and stopping related async operations.