From bda8007cdf4897e37ae94c34e23a1b9c3524af73 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 1 Sep 2023 07:33:30 -0400 Subject: [PATCH] refactor: make timeout a utility function --- src/service/registry.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/service/registry.ts b/src/service/registry.ts index 95aca55..64957be 100644 --- a/src/service/registry.ts +++ b/src/service/registry.ts @@ -136,7 +136,7 @@ export class RegistryService { return res; } this.sendRegistryRequest(pk); - await new Promise((resolve) => setTimeout(resolve, 200)); + await pTimeout(200); return this.getFromDB(pk); } else { this.sendRegistryRequest(pk); @@ -145,14 +145,14 @@ export class RegistryService { if (res === null) { this.logger.verbose(`[registry] get (clean) ${key}`); for (let i = 0; i < 200; i++) { - await new Promise((resolve) => setTimeout(resolve, 10)); + await pTimeout(10); if ((await this.getFromDB(pk)) !== null) { break; } } } else { this.logger.verbose(`[registry] get (cached) ${key}`); - await new Promise((resolve) => setTimeout(resolve, 200)); + await pTimeout(200); } return this.getFromDB(pk); } @@ -240,3 +240,7 @@ export class RegistryService { ]); } } + +async function pTimeout(ms: number) { + return new Promise((resolve) => setTimeout(resolve, ms)); +}