Jquery: $.ajax promises

Created on 17 Nov 2016  路  2Comments  路  Source: jquery/jquery

I see you have implemented the A+ promise in ajax but promises should only return one argument
What you have is jqXHR.then(function( data, textStatus, jqXHR ) {}

This prevents you from using async/await since only the first argument are assigned

let data = await jqXHR
// but what about textStatus and xhr?
// ideally 
let [data, textStatus, xhr] = await jqXHR
// but this means the promise must resolve an array... You could also resolve an object
let {data} = await jqXHR

I'm starting to use the fetch api more and more so $.ajax only feels like a extra dead wight... Would love it if you go more towards a fetch-alike api

Ajax

Most helpful comment

If you need jQuery.ajax, a workaround is to change:

let [data, textStatus, xhr] = await jqXHR;

to:

let [data, textStatus, xhr] = await jqXHR.then((...args) => args);

All 2 comments

We did explore a potential simpler jQuery.ajax replacement, called jQuery.xhr but we decided that it doesn't make sense to duplicate the Fetch API. If you use jQuery Slim instead of the full jQuery you'll have no AJAX & Effects modules but everything else. There's a window.fetch polyfill at https://github.com/whatwg/fetch.

As for making this specific change in jQuery.ajax, it would be a big breaking change, it would basically break all the code that used AJAX & attached success handlers to its promise. I don't think we can do that.

If you need jQuery.ajax, a workaround is to change:

let [data, textStatus, xhr] = await jqXHR;

to:

let [data, textStatus, xhr] = await jqXHR.then((...args) => args);
Was this page helpful?
0 / 5 - 0 ratings