refactor: make timeout a utility function

This commit is contained in:
Derrick Hammer 2023-09-01 07:33:30 -04:00
parent 9e03b62a59
commit bda8007cdf
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 7 additions and 3 deletions

View File

@ -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));
}