diff --git a/sdk/core/core-util/src/delay.ts b/sdk/core/core-util/src/delay.ts index 07f9b42a7534..1ebe336fbee0 100644 --- a/sdk/core/core-util/src/delay.ts +++ b/sdk/core/core-util/src/delay.ts @@ -2,9 +2,8 @@ // Licensed under the MIT license. import { AbortError, AbortSignalLike } from "@azure/abort-controller"; -import { isDefined } from "./typeGuards"; -const StandardAbortMessage = "The operation was aborted."; +const StandardAbortMessage = "The delay was aborted."; /** * Options for support abort functionality for the delay method @@ -28,38 +27,25 @@ export interface DelayOptions { */ export function delay(timeInMs: number, options?: DelayOptions): Promise { return new Promise((resolve, reject) => { - let timer: ReturnType | undefined = undefined; - let onAborted: (() => void) | undefined = undefined; - - const rejectOnAbort = (): void => { - return reject(new AbortError(options?.abortErrorMsg ?? StandardAbortMessage)); - }; - - const removeListeners = (): void => { - if (options?.abortSignal && onAborted) { - options.abortSignal.removeEventListener("abort", onAborted); - } - }; - - onAborted = (): void => { - if (isDefined(timer)) { - clearTimeout(timer); - } + function rejectOnAbort(): void { + reject(new AbortError(options?.abortErrorMsg ?? StandardAbortMessage)); + } + function removeListeners(): void { + options?.abortSignal?.removeEventListener("abort", onAbort); + } + function onAbort(): void { + // eslint-disable-next-line @typescript-eslint/no-use-before-define + clearTimeout(token); removeListeners(); - return rejectOnAbort(); - }; - - if (options?.abortSignal && options.abortSignal.aborted) { + rejectOnAbort(); + } + if (options?.abortSignal?.aborted) { return rejectOnAbort(); } - - timer = setTimeout(() => { + const token = setTimeout(() => { removeListeners(); resolve(); }, timeInMs); - - if (options?.abortSignal) { - options.abortSignal.addEventListener("abort", onAborted); - } + options?.abortSignal?.addEventListener("abort", onAbort); }); }