aurelia / http-client

A simple, restful, message-based wrapper around XMLHttpRequest.
MIT License
62 stars 59 forks source link

Abort works incorrectly for delayed requests #162

Open iyarkov opened 7 years ago

iyarkov commented 7 years ago

I'm submitting a bug report

Please tell us about your environment:

Current behavior:

An interceptor can delay XHR sending, and if the app cancels the request before it was send then the promise never resolved. HttpClient.pendingRequests collection grows with each request. Sample code:

initTest() {
    console.log('Test')
    this.testClient = new HttpClient();
    this.testClient.configure(config => {
        config.withInterceptor({
            request(message) {
                //Delay request for 1 second
                return new Promise((resolve, reject) => {
                    setTimeout(() => {resolve(message)}, 1000)
                })
            },
        });
    });
}

test() {
    console.log('Test')
    let promise = this.testClient.get('/test'); //preserve original promise with abort method
    promise.then(response => {
        console.log('response', response)
    })
    .catch(e => {
        console.log('error', e)
    })
    promise.abort()
    console.log('Pending requests', this.testClient.pendingRequests)
}

https://github.com/aurelia/http-client/blob/master/src/request-message-processor.js line 152 is invoked during processing phase.

if (this.isAborted) {
            // Some interceptors can delay sending of XHR, so when abort is called
            // before XHR is actually sent we abort() instead send()
            this.xhr.abort();
} else {

Line 140 - xhr.onabort = (e) => {never invoked

Expected/desired behavior: Expected result - promise either resolved or rejected

Alexander-Taran commented 3 years ago

@iyarkov not really a solution to XHR problem, but fetch with abortController works right. https://developer.mozilla.org/en-US/docs/Web/API/AbortController

Alexander-Taran commented 2 years ago

@bigopon maybe can be closed