kibana/test/common/services/retry/retry_for_truthy.ts
Brandon Kobel 4584a8b570
Elastic License 2.0 (#90099)
* Updating everything except the license headers themselves

* Applying ESLint rules

* Manually replacing the stragglers
2021-02-03 18:12:39 -08:00

44 lines
1.2 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { ToolingLog } from '@kbn/dev-utils';
import { retryForSuccess } from './retry_for_success';
interface Options {
timeout: number;
methodName: string;
description: string;
block: () => Promise<boolean>;
onFailureBlock?: () => Promise<any>;
}
export async function retryForTruthy(
log: ToolingLog,
{ timeout, methodName, description, block, onFailureBlock }: Options
) {
log.debug(`Waiting up to ${timeout}ms for ${description}...`);
await retryForSuccess(log, {
timeout,
methodName,
block,
onFailureBlock,
onFailure: (lastError) => {
let msg = `timed out waiting for ${description}`;
if (lastError) {
msg = `${msg} -- last error: ${lastError.stack || lastError.message}`;
}
throw new Error(msg);
},
accept: (result) => Boolean(result),
});
}