Skip to content

Commit 54b7354

Browse files
committed
fix: rejects if AbortSignal triggers in delay method
1 parent 9ad6b0d commit 54b7354

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

packages/shield-controller/src/polling-with-timeout-abort.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ export class PollingWithTimeoutAndAbort {
6363
abortController.abort();
6464
return result;
6565
} catch {
66-
if (abortController.signal.aborted) {
67-
// request failed due to the abort signal being triggered,
68-
// then we will break out of the polling loop
69-
break;
70-
}
7166
// otherwise, we will wait for the next polling cycle
7267
// and continue the polling loop
73-
await this.#delayWithAbortSignal(pollInterval, abortController.signal);
74-
continue;
68+
await this.#delayWithAbortSignal(
69+
pollInterval,
70+
abortController.signal,
71+
).catch(() => {
72+
// delayWithAbortSignal rejects, which means the abort signal was triggered
73+
// so we will break out of the polling loop
74+
});
7575
}
7676
}
7777
// At this point, the polling loop has exited and abortController is aborted
@@ -169,19 +169,23 @@ export class PollingWithTimeoutAndAbort {
169169
*
170170
* @param ms - The number of milliseconds to delay.
171171
* @param abortSignal - The abort signal to listen to.
172-
* @returns A promise that resolves when the delay is complete.
172+
* @returns A promise that resolves when the delay is complete or rejects if the abort signal is triggered.
173173
*/
174174
async #delayWithAbortSignal(ms: number, abortSignal: AbortSignal) {
175-
return new Promise((resolve) => {
175+
return new Promise((resolve, reject) => {
176176
let timer: NodeJS.Timeout | null = null;
177177

178+
if (abortSignal.aborted) {
179+
reject(new Error(this.ABORT_REASON_CANCELLED));
180+
}
181+
178182
const abortHandlerForDelay = () => {
179183
// clear the timeout and resolve the promise
180184
// Note: we don't reject the promise as this is only a dummy delay
181185
if (timer) {
182186
clearTimeout(timer);
183187
}
184-
resolve(undefined);
188+
reject(new Error(this.ABORT_REASON_CANCELLED));
185189
};
186190

187191
timer = setTimeout(() => {

0 commit comments

Comments
 (0)