From fccbfaccb88c7a66f86a2981b74445c894618a06 Mon Sep 17 00:00:00 2001 From: Ryan Lamb <4955475+kinyoklion@users.noreply.github.com> Date: Tue, 4 Jun 2024 10:01:46 -0700 Subject: [PATCH] fix: Cleanup timeout timer for waitForInitialization (#478) Use the new cancelable timer to ensure that the handle gets cleaned up. --- packages/shared/sdk-server/src/LDClientImpl.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/shared/sdk-server/src/LDClientImpl.ts b/packages/shared/sdk-server/src/LDClientImpl.ts index d5c31860b..de271ea36 100644 --- a/packages/shared/sdk-server/src/LDClientImpl.ts +++ b/packages/shared/sdk-server/src/LDClientImpl.ts @@ -1,5 +1,6 @@ /* eslint-disable class-methods-use-this */ import { + cancelableTimedPromise, ClientContext, Context, internal, @@ -11,7 +12,6 @@ import { LDTimeoutError, Platform, subsystem, - timedPromise, TypeValidators, } from '@launchdarkly/js-sdk-common'; @@ -911,8 +911,11 @@ export default class LDClientImpl implements LDClient { logger?: LDLogger, ): Promise { if (timeout) { - const timeoutPromise = timedPromise(timeout, 'waitForInitialization'); - return Promise.race([basePromise, timeoutPromise.then(() => this)]).catch((reason) => { + const cancelableTimeout = cancelableTimedPromise(timeout, 'waitForInitialization'); + return Promise.race([ + basePromise.then(() => cancelableTimeout.cancel()).then(() => this), + cancelableTimeout.promise.then(() => this), + ]).catch((reason) => { if (reason instanceof LDTimeoutError) { logger?.error(reason.message); }