NPM Resource Changed ? Pending

User tests: Successful: Unsuccessful:

avatar Fedik
Fedik
11 Jun 2022

Summary of Changes

Allow Joomla.request to return a Promise.
It more like RFC, not for 4.2, but we can put it in one of future version.

I have added new option promise, when it is true then Joomla.request will return a Promise.
So it can be used as:

Joomla.request({
    url: 'blabla/file.json'
    promise: true
})
  .then((xhr) => {
    console.log('ok', xhr.responseText);
  })
  .catch((xhr) => {
    console.error('error', xhr.statusText);
  });

Testing Instructions

Run npm install.
Make sure all AJAX stuf works as before, for example extensions upload.

Then add to index.php of any template:

<script type="module">
  Joomla.request({
    url: Joomla.getOptions('system.paths').base + '/index.php?option=com_ajax&type=plugin',
    promise: true
  })
    .then(() => console.log('success'))
    .catch(() => console.log('error'));
    
  Joomla.request({
    url: Joomla.getOptions('system.paths').base + '/index.php?option=com_ajax&type=plugin&format=raw',
    promise: true
  })
    .then(() => console.log('success'))
    .catch(() => console.log('error'));
</script>

Watch result in the browser console.

Actual result BEFORE applying this Pull Request

2 message (1 per request):

error
success

Expected result AFTER applying this Pull Request

2 message (1 per request):

error
success

Documentation Changes Required

yeah, if it exists somwhere,
I have updated comment for Joomla.request method description.

avatar Fedik Fedik - open - 11 Jun 2022
avatar Fedik Fedik - change - 11 Jun 2022
Status New Pending
avatar joomla-cms-bot joomla-cms-bot - change - 11 Jun 2022
Category JavaScript Repository NPM Change
avatar HLeithner
HLeithner - comment - 27 Jun 2022

@Fedik can you please fix the conflict, thanks

avatar Fedik
Fedik - comment - 28 Jun 2022

Hm, currently it not really possible, because #38019

avatar Fedik Fedik - close - 28 Jun 2022
avatar Fedik Fedik - change - 28 Jun 2022
Status Pending Closed
Closed_Date 0000-00-00 00:00:00 2022-06-28 08:53:42
Closed_By Fedik
Labels Added: NPM Resource Changed ?
avatar HLeithner
HLeithner - comment - 28 Jun 2022

Because of the queue handling in this function? I didn't read the whole thread so sorry if I miss something.

Wouldn't make more sense to have an own function that handles the queue and return a promise which then uses the Request method?

avatar Fedik
Fedik - comment - 28 Jun 2022

Because of the queue handling in this function?

yes

Wouldn't make more sense to have an own function that handles the queue and return a promise which then uses the Request method?

And that first what I have sugested in that PR: "use own method that handles the queue" :)

Well, I will look later, maybe will find another way.

avatar Fedik
Fedik - comment - 28 Jun 2022

@nikosdion what do you think about reimplementing #38019 using #38019 (comment) with changes from this PR?

yeah I remember I talked about new method for promise, but seems it not that hard in existing method :)

avatar nikosdion
nikosdion - comment - 28 Jun 2022

@Fedik When I tried this method it did not work AT ALL. I explained why this is the case in #38019 (comment) You seem to also be forcing perform=true when a promise is returned, just like the Fetch API, therefore we'd never be able to set up the queue. Everything would execute at one, so we're back where we started as if 38019 never happened at all.

avatar Fedik
Fedik - comment - 28 Jun 2022

Yeah, the javascript promisses is eagar not lazy.

hmhm, I thought this one would work, where you create a next promise only after previous is resolved:

let lastRequestPromise = Promise.resolve();

Joomla.enqeueRequest = (options) => {
   options.promise = true;
   lastRequestPromise = lastRequestPromise.then(() => Joomla.Request(options));
}

Well, okay

avatar nikosdion
nikosdion - comment - 28 Jun 2022

You can create a sequence with promises but it requires knowing all of the queue items in advance, see https://web.dev/promises/#creating-a-sequence

Our problem is that we cannot know our queue items in advance. They might even be added at runtime as a result of a previous queue item executing or some conditional JavaScript. As a result we cannot use this trick for creating a sequence of web requests with promises.

Adding Promises support to Joomla.Request is not useless, though! There are some cases where you want to run several requests in parallel and take action when all of them have completed, the JS equivalent of using curl_multi_init() in PHP code.

Add a Comment

Login with GitHub to post a comment