@camunda8/sdk
Version:
[](https://www.npmjs.com/package/@camunda8/sdk)
55 lines (54 loc) • 2.49 kB
TypeScript
interface PollingOperationOptionsBase<T> {
operation: () => Promise<T>;
/** how often to poll in ms - defaults to 1000 */
interval?: number;
/** when to timeout - defaults to 30000 */
timeout?: number;
}
interface PollingOperationOptionsWithPredicate<T> extends PollingOperationOptionsBase<T> {
/** predicate to check if the result is valid */
predicate: (result: T) => boolean;
}
interface PollingOperationOptionsWithoutPredicate<T extends {
items: Array<unknown>;
}> extends PollingOperationOptionsBase<T> {
/** predicate to check if the result is valid - optional when T has items array */
predicate?: (result: T) => boolean;
}
/**
* Poll for a result of an operation until it returns an awaited result or times out.
* This is useful for operations that may take some time to complete, such as waiting for a process instance to finish or data to propagate to query indices.
* Takes an optional prediicate function to determine if the result is the awaited one. By default, it checks if the result is not null or undefined and has at least one item in the `items` array.
* @param options options for the polling operation
* @returns either the result of the operation or an error if the operation times out. If results were returned, but the predicate was not met, a PredicateError is thrown.
* Otherwise, the failure is propagated as an error.
* @example
* ```ts
* // Wait for a process instance to appear in the search results
* const elementInstances = await PollingOperation({
* operation: () =>
* c8.searchElementInstances({
* sort: [{ field: 'processInstanceKey' }],
* filter: {
* processInstanceKey: processInstance.processInstanceKey,
* type: 'SERVICE_TASK',
* },
* }),
* interval: 500,
* timeout: 10000,
* })
*
* // If the operation does not return an object with an `items` array (ie: a v1 API), you need to provide a predicate function to check if the result is the awaited one.
* const process = await PollingOperation({
* operation: () => c.getProcessInstance(p.processInstanceKey),
* predicate: (res) => res.key === p.processInstanceKey,
* interval: 500,
* timeout: 15000,
* })
*```
*/
export declare function PollingOperation<T extends {
items: Array<unknown>;
}>(options: PollingOperationOptionsWithoutPredicate<T>): Promise<T>;
export declare function PollingOperation<T>(options: PollingOperationOptionsWithPredicate<T>): Promise<T>;
export {};