diff --git a/lib/conditional.module.ts b/lib/conditional.module.ts index 41875988..f7d70545 100644 --- a/lib/conditional.module.ts +++ b/lib/conditional.module.ts @@ -13,14 +13,11 @@ export class ConditionalModule { condition: string | ((env: NodeJS.ProcessEnv) => boolean), options?: { timeout: number }, ) { - let configResolved = false; const { timeout = 5000 } = options ?? {}; - setTimeout(() => { - if (!configResolved) { - throw new Error( - `Nest was not able to resolve the config variables within ${timeout} milliseconds. Bause of this, the ConditionalModule was not able to determine if ${module.toString()} should be registered or not`, - ); - } + const timer = setTimeout(() => { + throw new Error( + `Nest was not able to resolve the config variables within ${timeout} milliseconds. Bause of this, the ConditionalModule was not able to determine if ${module.toString()} should be registered or not`, + ); }, timeout); const returnModule: Required< Pick @@ -32,7 +29,7 @@ export class ConditionalModule { }; } await ConfigModule.envVariablesLoaded; - configResolved = true; + clearTimeout(timer); const evaluation = condition(process.env); if (evaluation) { returnModule.imports.push(module);