Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject requests with AbortSignal reason #1436

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ export function fetch(input, init) {
var request = new Request(input, init)

if (request.signal && request.signal.aborted) {
return reject(new DOMException('Aborted', 'AbortError'))
return reject(request.signal.reason);
}

var xhr = new XMLHttpRequest()
Expand Down Expand Up @@ -570,7 +570,7 @@ export function fetch(input, init) {

xhr.onabort = function() {
setTimeout(function() {
reject(new DOMException('Aborted', 'AbortError'))
reject(request.signal ? request.signal.reason : new DOMException('Aborted', 'AbortError'))
}, 0)
}

Expand Down
110 changes: 105 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ exercise.forEach(function(exerciseMode) {
assert.deepEqual(controller.signal, request.signal);
})

test('initially aborted signal', function () {
test('initially aborted signal without reason', function () {
var controller = new AbortController()
controller.abort()

Expand All @@ -1221,8 +1221,24 @@ exercise.forEach(function(exerciseMode) {
}
)
})

test('initially aborted signal with reason', function () {
var controller = new AbortController()
controller.abort('Something bad happened')

test('initially aborted signal within Request', function() {
return fetch('/request', {
signal: controller.signal
}).then(
function() {
assert.ok(false)
},
function(error) {
assert.equal(error, 'Something bad happened')
}
)
})

test('initially aborted signal without reason within Request', function() {
var controller = new AbortController()
controller.abort()

Expand All @@ -1238,7 +1254,23 @@ exercise.forEach(function(exerciseMode) {
)
})

test('mid-request', function() {
test('initially aborted signal with reason within Request', function() {
var controller = new AbortController()
controller.abort('Something bad happened')

var request = new Request('/request', {signal: controller.signal})

return fetch(request).then(
function() {
assert.ok(false)
},
function(error) {
assert.equal(error, 'Something bad happened')
}
)
})

test('mid-request without reason', function() {
var controller = new AbortController()

setTimeout(function() {
Expand All @@ -1256,8 +1288,27 @@ exercise.forEach(function(exerciseMode) {
}
)
})

test('mid-request with reason', function() {
var controller = new AbortController()

setTimeout(function() {
controller.abort('Something bad happened')
}, 30)

return fetch('/slow?_=' + new Date().getTime(), {
signal: controller.signal
}).then(
function() {
assert.ok(false)
},
function(error) {
assert.equal(error, 'Something bad happened')
}
)
})

test('mid-request within Request', function() {
test('mid-request without reason within Request', function() {
var controller = new AbortController()
var request = new Request('/slow?_=' + new Date().getTime(), {signal: controller.signal})

Expand All @@ -1275,7 +1326,25 @@ exercise.forEach(function(exerciseMode) {
)
})

test('abort multiple with same signal', function() {
test('mid-request with reason within Request', function() {
var controller = new AbortController()
var request = new Request('/slow?_=' + new Date().getTime(), {signal: controller.signal})

setTimeout(function() {
controller.abort('Something bad happened')
}, 30)

return fetch(request).then(
function() {
assert.ok(false)
},
function(error) {
assert.equal(error, 'Something bad happened')
}
)
})

test('abort multiple without reason with same signal', function() {
var controller = new AbortController()

setTimeout(function() {
Expand Down Expand Up @@ -1305,6 +1374,37 @@ exercise.forEach(function(exerciseMode) {
)
])
})

test('abort multiple with reason with same signal', function() {
var controller = new AbortController()

setTimeout(function() {
controller.abort('Something bad happened')
}, 30)

return Promise.all([
fetch('/slow?_=' + new Date().getTime(), {
signal: controller.signal
}).then(
function() {
assert.ok(false)
},
function(error) {
assert.equal(error, 'Something bad happened')
}
),
fetch('/slow?_=' + new Date().getTime(), {
signal: controller.signal
}).then(
function() {
assert.ok(false)
},
function(error) {
assert.equal(error, 'Something bad happened')
}
)
])
})
})

suite('response', function() {
Expand Down
Loading