diff --git a/x-pack/test/detection_engine_api_integration/utils.ts b/x-pack/test/detection_engine_api_integration/utils.ts index f8989c685c82c..e187d9fff0348 100644 --- a/x-pack/test/detection_engine_api_integration/utils.ts +++ b/x-pack/test/detection_engine_api_integration/utils.ts @@ -766,26 +766,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}`); + } }; /**