Skip to content

Commit 83c23c0

Browse files
authored
fix(typescript): clear timeout handle after fetch (#32)
1 parent 721c156 commit 83c23c0

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

xdk-gen/templates/typescript/http_client.j2

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,28 @@ export class HttpClient {
116116

117117
// Handle timeout
118118
let signal = options.signal;
119+
let timeoutId: ReturnType<typeof setTimeout> | undefined;
120+
119121
if (options.timeout && options.timeout > 0 && !signal) {
120122
const controller = new AbortController();
121-
setTimeout(() => controller.abort(), options.timeout);
123+
timeoutId = setTimeout(() => controller.abort(), options.timeout);
122124
signal = controller.signal;
123125
}
124126

125-
const response = await this.fetch(url, {
126-
method: options.method || 'GET',
127-
headers: options.headers as any,
128-
body: body as any,
129-
signal: signal,
130-
});
131-
132-
return response as HttpResponse;
127+
try {
128+
const response = await this.fetch(url, {
129+
method: options.method || 'GET',
130+
headers: options.headers as any,
131+
body: body as any,
132+
signal: signal,
133+
});
134+
135+
return response as HttpResponse;
136+
} finally {
137+
if (timeoutId !== undefined) {
138+
clearTimeout(timeoutId);
139+
}
140+
}
133141
}
134142

135143
/**

0 commit comments

Comments
 (0)