Skip to content

Commit 1f903ed

Browse files
authoredApr 13, 2025··
Improve reliability of cloned request cleanup (#693)
1 parent 93c02ac commit 1f903ed

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed
 

‎source/core/Ky.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ export class Ky {
6666
throw error;
6767
}
6868

69-
// Now that we know a retry is not needed, close the ReadableStream of the cloned request.
70-
if (!ky.request.bodyUsed) {
71-
await ky.request.body?.cancel();
72-
}
73-
7469
// If `onDownloadProgress` is passed, it uses the stream API internally
7570
if (ky._options.onDownloadProgress) {
7671
if (typeof ky._options.onDownloadProgress !== 'function') {
@@ -88,7 +83,13 @@ export class Ky {
8883
};
8984

9085
const isRetriableMethod = ky._options.retry.methods.includes(ky.request.method.toLowerCase());
91-
const result = (isRetriableMethod ? ky._retry(function_) : function_()) as ResponsePromise;
86+
const result = (isRetriableMethod ? ky._retry(function_) : function_())
87+
.finally(async () => {
88+
// Now that we know a retry is not needed, close the ReadableStream of the cloned request.
89+
if (!ky.request.bodyUsed) {
90+
await ky.request.body?.cancel();
91+
}
92+
}) as ResponsePromise;
9293

9394
for (const [type, mimeType] of Object.entries(responseTypes) as ObjectEntries<typeof responseTypes>) {
9495
result[type] = async () => {

0 commit comments

Comments
 (0)
Please sign in to comment.