Skip to content

Commit

Permalink
fix(health): open handle when using pingCheck in jest environment
Browse files Browse the repository at this point in the history
resolves #1466
  • Loading branch information
BrunnerLivio committed Nov 26, 2021
1 parent 4e92162 commit 0d7b48e
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lib/utils/promise-timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ export class TimeoutError extends Error {}
*
* @internal
*/
export const promiseTimeout = function(
export const promiseTimeout = function (
ms: number,
promise: Promise<any>,
): Promise<any> {
// Create a promise that rejects in <ms> milliseconds
let timeout = new Promise((_, reject) => {
let id = setTimeout(() => {
clearTimeout(id);
reject(new TimeoutError('Timed out in ' + ms + 'ms.'));
}, ms);
});

// Returns a race between our timeout and the passed in promise
return Promise.race([promise, timeout]);
let timer: NodeJS.Timeout;
return Promise.race([
promise,
new Promise(
(_, reject) =>
(timer = setTimeout(
() => reject(new TimeoutError(`Timed out in ${ms}ms.`)),
ms,
)),
),
]).finally(() => clearTimeout(timer));
};

0 comments on commit 0d7b48e

Please sign in to comment.