Skip to content

Commit

Permalink
cancel http client timeout promise on successful response #172
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Jan 22, 2024
1 parent 40d3bee commit fe44436
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/main/ts/common/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,16 @@ export default class HttpClient {
// request using fetch or xhr with timeout
let timeout = request.timeout === undefined ? HttpClient.DEFAULT_TIMEOUT : request.timeout === 0 ? HttpClient.MAX_TIMEOUT : request.timeout;
let requestPromise = request.requestApi === "fetch" ? HttpClient.requestFetch(request) : HttpClient.requestXhr(request);
let timeoutId;
let timeoutPromise = new Promise((resolve, reject) => {
let id = setTimeout(() => {
clearTimeout(id);
timeoutId = setTimeout(() => {
clearTimeout(timeoutId);
reject('Request timed out in '+ timeout + ' milliseconds')
}, timeout);
});
return Promise.race([requestPromise, timeoutPromise]);
let response = await Promise.race([requestPromise, timeoutPromise]);
clearTimeout(timeoutId);
return response;
}

// ----------------------------- PRIVATE HELPERS ----------------------------
Expand Down

0 comments on commit fe44436

Please sign in to comment.