Page MenuHomePhabricator

Provide an API for external JavaScript source load
Closed, DuplicatePublic

Description

Don't know if that is intentional (due to some policy) but am asking, can you provide or extend mw.loader.load('URL') API to return promise (or there is another for this already?)? See T215280 for example as I couldn't use the source directly I had to directly use

function loadScriptAsync(uri) {

return new Promise(function (resolve, reject) {

		var tag = document.createElement('script');
		tag.src = uri;
		tag.async = true;
		tag.onload = resolve;
		var firstScriptTag = document.getElementsByTagName('script')[0];
		firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

});

}

To load my script on https://fa.wikipedia.org/wiki/MediaWiki:Experimental-react-demo.js which easily can be written using $.Deferred in order to have compatibility with pre-ES6 we engines. Thanks!

Event Timeline

Ebraminio created this task.Feb 5 2019, 4:10 PM
Restricted Application added a subscriber: Aklapper. · View Herald TranscriptFeb 5 2019, 4:10 PM
Ebraminio renamed this task from Provide promise for external source load to Provide an API for external JavaScript source load.Feb 5 2019, 4:11 PM
Restricted Application added a project: Performance-Team. · View Herald TranscriptFeb 5 2019, 5:16 PM

Quick summary of current state:

  • mw.loader.load() supports URLs but doesn't return a promise
  • mw.loader.using() returns a promise, but doesn't support URLs

This task could be resolved by either adding a promise return to load(), or adding support for URLs to using().

Task T27962 proposes the latter, so I'll merge them. (There is also a recently submitted patch there!)

Krinkle added a subscriber: Krinkle.Feb 5 2019, 8:27 PM

@Ebraminio The solution for today is as follows:

- var promise = loadScriptAsync( 'https://my.test/url.js' );
+ var promise = $.ajax('https://my.test/url.js', { dataType: 'script', cache: true });