Skip to content

Commit

Permalink
fix: abort request on timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
mohdashraf010897 committed Jul 21, 2023
1 parent ea23cab commit f43e6b6
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/core/request/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ function fetchRequest(args) {
const transformedRequest = Object.assign({}, ts);
const { url } = transformedRequest;
delete transformedRequest.url;

const controller = new AbortController();
const { signal } = controller;

const fetchPromise = fetch(
url || finalURL,
Object.assign({}, transformedRequest, {
Expand All @@ -111,13 +115,15 @@ function fetchRequest(args) {
'x-timestamp': new Date().getTime(),
})
: transformedRequest.headers,
signal, // Attach the abort signal to the fetch request
}),
);

const timeoutPromise = new Promise((_, rejectTP) => {
if (httpRequestTimeout > 0) {
setTimeout(() => {
rejectTP(new Error('Request timeout'));
controller.abort();
}, httpRequestTimeout);
}
});
Expand Down

0 comments on commit f43e6b6

Please sign in to comment.