Skip to content

Commit

Permalink
Merge pull request #1399 from mattridley/master
Browse files Browse the repository at this point in the history
Fixing #537 Rejecting promise if request is cancelled by the browser
  • Loading branch information
nickuraltsev committed Mar 7, 2018
2 parents d4dc124 + 980065d commit 0499a97
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/adapters/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ module.exports = function xhrAdapter(config) {
request = null;
};

// Handle browser request cancellation (as opposed to a manual cancellation)
request.onabort = function handleAbort() {
if (!request) {
return;
}

reject(createError('Request aborted', config, 'ECONNABORTED', request));

// Clean up request
request = null;
};

// Handle low level network errors
request.onerror = function handleError() {
// Real errors are hidden from us by the browser
Expand Down
25 changes: 25 additions & 0 deletions test/specs/requests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,31 @@ describe('requests', function () {
.then(finish, finish);
});

it('should reject on abort', function (done) {
var resolveSpy = jasmine.createSpy('resolve');
var rejectSpy = jasmine.createSpy('reject');

var finish = function () {
expect(resolveSpy).not.toHaveBeenCalled();
expect(rejectSpy).toHaveBeenCalled();
var reason = rejectSpy.calls.first().args[0];
expect(reason instanceof Error).toBe(true);
expect(reason.config.method).toBe('get');
expect(reason.config.url).toBe('/foo');
expect(reason.request).toEqual(jasmine.any(XMLHttpRequest));

done();
};

axios('/foo')
.then(resolveSpy, rejectSpy)
.then(finish, finish);

getAjaxRequest().then(function (request) {
request.abort();
});
});

it('should reject when validateStatus returns false', function (done) {
var resolveSpy = jasmine.createSpy('resolve');
var rejectSpy = jasmine.createSpy('reject');
Expand Down

0 comments on commit 0499a97

Please sign in to comment.