mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-25 12:35:01 +01:00
28 lines
899 B
JavaScript
28 lines
899 B
JavaScript
export function abortOnTimeout(createTimeout, timeoutAmount, requestResult, responsePromise) {
|
|
const timeout = createTimeout(timeoutAmount);
|
|
// abort request if timeout finishes first
|
|
let timedOut = false;
|
|
timeout.elapsed().then(
|
|
() => {
|
|
timedOut = true;
|
|
requestResult.abort();
|
|
},
|
|
() => {} // ignore AbortError when timeout is aborted
|
|
);
|
|
// abort timeout if request finishes first
|
|
return responsePromise.then(
|
|
response => {
|
|
timeout.abort();
|
|
return response;
|
|
},
|
|
err => {
|
|
timeout.abort();
|
|
// map error to TimeoutError
|
|
if (err instanceof AbortError && timedOut) {
|
|
throw new ConnectionError(`Request timed out after ${timeoutAmount}ms`, true);
|
|
} else {
|
|
throw err;
|
|
}
|
|
}
|
|
);
|
|
} |