apps-script-utils 2.1.1 Help

retry

function retry<T>(fn: (attempt: number) => T, options: RetryOptions = {}): T;

The wait starts at delay, is multiplied by multiplier after each failure and never exceeds maxDelay. shouldRetry decides whether a given failure is worth repeating at all — a rate limit usually is, a bad argument never is.

The function receives the attempt number, starting at 1. When the last attempt fails its error is thrown on. The wait itself goes through sleep, which defaults to Utilities.sleep where that exists and can be replaced in a test.

Parameters

Parameter

Type

Description

fn

(attempt: number) => T

The function to call. It receives the attempt number.

options (optional) = {}

RetryOptions

attempts, delay, multiplier, maxDelay, shouldRetry and sleep.

Returns

T — whatever the function returns on the attempt that succeeds.

Throws

Exception

Condition

IllegalArgumentException

the first argument is not a function, or an option is out of range.

Examples

Retrying a flaky call

const response = retry(() => UrlFetchApp.fetch(url), { attempts: 4, delay: 500, multiplier: 2, shouldRetry: (error) => String(error).includes("429") });

See also

Source

src/lang/function/retry.ts

23 September 2026

This documentation was generated with AI (Claude) from the library's source code.