sdk.automation.retry
Home > @matterway/sdk > Automation > retry
Automation.retry() function
Runs a function and retries it on exception.
Signature:
export declare function retry<T>(ctx: Context, fn: () => Promise<T>, options?: RetryOptions): Promise<T>;
Parameters
Parameter | Type | Description |
---|---|---|
ctx | Context | The context for automation. |
fn | () => Promise<T> | A function that should be retried on exception. |
options | RetryOptions | (Optional) Options for retry. |
Returns:
Promise<T>
The return value of the function.
Remarks
Runs a function and retries it on exception up to maxTries
times, after which it will reject with the last thrown exception, and otherwise return the original result.
Most often, the retry function should contain not only the action that can fail, but also its success condition (eg. a particular selector appears, or the response does not contain errors) which checks if the action should be repeated. To cause a retry, simply throw anywhere from the retry function.
This makes certain operations more resilient, like clicking on unresponsive user interfaces, making navigation or API requests to unstable systems, or retrying entire transactions.
Example 1
await retry(ctx, async () => {
await click(ctx, '.button');
await waitForSelector(ctx, '.success.notification', {timeout: 3000});
});
Example 2
const value = await retry(ctx, async () => {
await click(ctx, '.button');
return getValue(ctx, '.result');
}, {
maxTries: 3,
retryDelay: 500,
});