Skip to content

Commit 8462d79

Browse files
steveluscherJakeChampion
authored andcommitted
Reject requests with AbortSignal reason
1 parent ba5cf1e commit 8462d79

File tree

2 files changed

+107
-7
lines changed

2 files changed

+107
-7
lines changed

fetch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ export function fetch(input, init) {
528528
var request = new Request(input, init)
529529

530530
if (request.signal && request.signal.aborted) {
531-
return reject(new DOMException('Aborted', 'AbortError'))
531+
return reject(request.signal.reason);
532532
}
533533

534534
var xhr = new XMLHttpRequest()
@@ -570,7 +570,7 @@ export function fetch(input, init) {
570570

571571
xhr.onabort = function() {
572572
setTimeout(function() {
573-
reject(new DOMException('Aborted', 'AbortError'))
573+
reject(request.signal ? request.signal.reason : new DOMException('Aborted', 'AbortError'))
574574
}, 0)
575575
}
576576

test/test.js

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ exercise.forEach(function(exerciseMode) {
12051205
assert.deepEqual(controller.signal, request.signal);
12061206
})
12071207

1208-
test('initially aborted signal', function () {
1208+
test('initially aborted signal without reason', function () {
12091209
var controller = new AbortController()
12101210
controller.abort()
12111211

@@ -1221,8 +1221,24 @@ exercise.forEach(function(exerciseMode) {
12211221
}
12221222
)
12231223
})
1224+
1225+
test('initially aborted signal with reason', function () {
1226+
var controller = new AbortController()
1227+
controller.abort('Something bad happened')
12241228

1225-
test('initially aborted signal within Request', function() {
1229+
return fetch('/request', {
1230+
signal: controller.signal
1231+
}).then(
1232+
function() {
1233+
assert.ok(false)
1234+
},
1235+
function(error) {
1236+
assert.equal(error, 'Something bad happened')
1237+
}
1238+
)
1239+
})
1240+
1241+
test('initially aborted signal without reason within Request', function() {
12261242
var controller = new AbortController()
12271243
controller.abort()
12281244

@@ -1238,7 +1254,23 @@ exercise.forEach(function(exerciseMode) {
12381254
)
12391255
})
12401256

1241-
test('mid-request', function() {
1257+
test('initially aborted signal with reason within Request', function() {
1258+
var controller = new AbortController()
1259+
controller.abort('Something bad happened')
1260+
1261+
var request = new Request('/request', {signal: controller.signal})
1262+
1263+
return fetch(request).then(
1264+
function() {
1265+
assert.ok(false)
1266+
},
1267+
function(error) {
1268+
assert.equal(error, 'Something bad happened')
1269+
}
1270+
)
1271+
})
1272+
1273+
test('mid-request without reason', function() {
12421274
var controller = new AbortController()
12431275

12441276
setTimeout(function() {
@@ -1256,8 +1288,27 @@ exercise.forEach(function(exerciseMode) {
12561288
}
12571289
)
12581290
})
1291+
1292+
test('mid-request with reason', function() {
1293+
var controller = new AbortController()
1294+
1295+
setTimeout(function() {
1296+
controller.abort('Something bad happened')
1297+
}, 30)
1298+
1299+
return fetch('/slow?_=' + new Date().getTime(), {
1300+
signal: controller.signal
1301+
}).then(
1302+
function() {
1303+
assert.ok(false)
1304+
},
1305+
function(error) {
1306+
assert.equal(error, 'Something bad happened')
1307+
}
1308+
)
1309+
})
12591310

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

@@ -1275,7 +1326,25 @@ exercise.forEach(function(exerciseMode) {
12751326
)
12761327
})
12771328

1278-
test('abort multiple with same signal', function() {
1329+
test('mid-request with reason within Request', function() {
1330+
var controller = new AbortController()
1331+
var request = new Request('/slow?_=' + new Date().getTime(), {signal: controller.signal})
1332+
1333+
setTimeout(function() {
1334+
controller.abort('Something bad happened')
1335+
}, 30)
1336+
1337+
return fetch(request).then(
1338+
function() {
1339+
assert.ok(false)
1340+
},
1341+
function(error) {
1342+
assert.equal(error, 'Something bad happened')
1343+
}
1344+
)
1345+
})
1346+
1347+
test('abort multiple without reason with same signal', function() {
12791348
var controller = new AbortController()
12801349

12811350
setTimeout(function() {
@@ -1305,6 +1374,37 @@ exercise.forEach(function(exerciseMode) {
13051374
)
13061375
])
13071376
})
1377+
1378+
test('abort multiple with reason with same signal', function() {
1379+
var controller = new AbortController()
1380+
1381+
setTimeout(function() {
1382+
controller.abort('Something bad happened')
1383+
}, 30)
1384+
1385+
return Promise.all([
1386+
fetch('/slow?_=' + new Date().getTime(), {
1387+
signal: controller.signal
1388+
}).then(
1389+
function() {
1390+
assert.ok(false)
1391+
},
1392+
function(error) {
1393+
assert.equal(error, 'Something bad happened')
1394+
}
1395+
),
1396+
fetch('/slow?_=' + new Date().getTime(), {
1397+
signal: controller.signal
1398+
}).then(
1399+
function() {
1400+
assert.ok(false)
1401+
},
1402+
function(error) {
1403+
assert.equal(error, 'Something bad happened')
1404+
}
1405+
)
1406+
])
1407+
})
13081408
})
13091409

13101410
suite('response', function() {

0 commit comments

Comments
 (0)