Abort during delay
This commit is contained in:
@@ -263,7 +263,31 @@ export class ExponentialBackoff {
|
||||
|
||||
// Wait before going to the next attempt
|
||||
const delay = this.#calculateDelay(this.#options, attempt);
|
||||
await new Promise((resolve) => setTimeout(resolve, delay));
|
||||
|
||||
// Wait for the delay or the abort signal
|
||||
await new Promise((resolve, reject) => {
|
||||
// Set a timeout to resolve the promise after the delay
|
||||
let timeout: ReturnType<typeof setTimeout>;
|
||||
|
||||
// Handle the abort signal
|
||||
const abortHandler = () => {
|
||||
clearTimeout(timeout);
|
||||
abortController.signal.removeEventListener('abort', abortHandler);
|
||||
reject(new ExponentialBackoffStoppedRetriesError(abortController.signal.reason));
|
||||
};
|
||||
|
||||
// Handle the timeout
|
||||
const timeoutHandler = () => {
|
||||
abortController.signal.removeEventListener('abort', abortHandler);
|
||||
resolve(undefined);
|
||||
};
|
||||
|
||||
// Set the timeout
|
||||
timeout = setTimeout(timeoutHandler, delay);
|
||||
|
||||
// Add the abort handler to the abort signal
|
||||
abortController.signal.addEventListener('abort', abortHandler);
|
||||
})
|
||||
|
||||
attempt++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user