From eeb3362d91639d6e4328a2dc5b51bb8e27da6991 Mon Sep 17 00:00:00 2001 From: Spencer Date: Mon, 23 Aug 2021 11:38:01 -0700 Subject: [PATCH] [tests] prevent unhandled rejection (#109560) Co-authored-by: spalger Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../detection_engine_api_integration/utils.ts | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/x-pack/test/detection_engine_api_integration/utils.ts b/x-pack/test/detection_engine_api_integration/utils.ts index a067ac5974ac6..a851b838867a1 100644 --- a/x-pack/test/detection_engine_api_integration/utils.ts +++ b/x-pack/test/detection_engine_api_integration/utils.ts @@ -767,26 +767,22 @@ export const waitFor = async ( maxTimeout: number = 20000, timeoutWait: number = 10 ): Promise => { - await new Promise(async (resolve, reject) => { - let found = false; - let numberOfTries = 0; - while (!found && numberOfTries < Math.floor(maxTimeout / timeoutWait)) { - const itPasses = await functionToTest(); - if (itPasses) { - found = true; - } else { - numberOfTries++; - } - await new Promise((resolveTimeout) => setTimeout(resolveTimeout, timeoutWait)); - } - if (found) { - resolve(); + let found = false; + let numberOfTries = 0; + + while (!found && numberOfTries < Math.floor(maxTimeout / timeoutWait)) { + if (await functionToTest()) { + found = true; } else { - reject( - new Error(`timed out waiting for function condition to be true within ${functionName}`) - ); + numberOfTries++; } - }); + + await new Promise((resolveTimeout) => setTimeout(resolveTimeout, timeoutWait)); + } + + if (!found) { + throw new Error(`timed out waiting for function condition to be true within ${functionName}`); + } }; /**