canjs / can-ajax

jQuery-inspired AJAX request library.
https://canjs.com/doc/can-ajax.html
MIT License
5 stars 7 forks source link

RFC: E6 destructuring for resolving promise #39

Open pYr0x opened 6 years ago

pYr0x commented 6 years ago

i would like to unify the response of the XHR response. the idea is to response a array while resolving or rejecting. i came up with this:

xhr.onreadystatechange = function () {
        try {
            if (xhr.readyState === 4) {
                if (timer) {
                    clearTimeout(timer);
                }
                if (xhr.status < 300) {
                    if (o.success) {
                        o.success(_xhrResp(xhr, o), xhr.statusText, xhr);
                    }
                }
                else if (o.error) {
                    o.error(_xhrResp(xhr, o), xhr.statusText, xhr);
                }
                if (o.complete) {
                    o.complete(_xhrResp(xhr, o), xhr.statusText, xhr);
                }

                if (xhr.status >= 200 && xhr.status < 300) {
                    deferred.resolve([_xhrResp(xhr, o), xhr.statusText, xhr]);
                } else {
                    deferred.reject([_xhrResp(xhr, o), xhr.statusText, xhr]);
                }
            }
            else if (o.progress) {
                o.progress(++n);
            }
        } catch(e) {
            deferred.reject(e);
        }
    };

deferred.resolve([_xhrResp(xhr, o), xhr.statusText, xhr]); will return an array which can be destructured

ajax({
            type: "get",
            url: __dirname+"/can-ajax-test-result.json"
        }).then(function([resp, textStatus, xhr]){
            assert.equal(textStatus, "OK");
            done();
        });

this change will break all tests and all implementations this change will only work in Edge. Not on IE11

any thoughts?

justinbmeyer commented 6 years ago

Could this be behind a flag so as not to break existing apps?

justinbmeyer commented 6 years ago

Why wouldn’t this work in IE?

pYr0x commented 6 years ago

destructuring is not working on IE https://kangax.github.io/compat-table/es6/

justinbmeyer commented 6 years ago

Yeah, but you IE could destructure manually or with a transpiler right?

Sent from my iPhone

On Jun 9, 2018, at 10:02 AM, Julian notifications@github.com wrote:

destructuring is not working on IE https://kangax.github.io/compat-table/es6/

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.