Skip to content

retry

Runs the given function and, if it fails, runs it again until it succeeds or the attempts run out. The value of the first successful attempt is returned; if every attempt fails, the error of the last one is raised.

times counts total attempts, not extra ones, so the default of 3 means one call plus at most two retries and times: 1 disables retrying altogether. times below 1 is an error.

delay is the wait between two attempts, in milliseconds, and backoff multiplies it after each failure — delay: 100, backoff: 2 waits 100ms, then 200ms, then 400ms. The wait sits strictly between attempts, so the last failure is reported without waiting one more time for nothing.

The function may be synchronous or asynchronous, and retry itself is asynchronous.

retry is synchronous and waits with time.sleep, exactly as sleep does.

Parameters

NameTypeRequiredDefault
funcfunctionFunctionCallable
The function to run. Its return value is passed straight back to the caller.
optionsnamedkeywordRetryOptions
See the table below.

namedThese parameters are passed as named parameters.

keywordThese parameters are passed as keyword arguments, or as a single dict in their place.

RetryOptions
NameTypeRequiredDefault
timesnumberint3
Total number of attempts, including the first one. Must be at least 1.
delaynumberint0
Milliseconds to wait between two attempts.
backoffnumberint1
Multiplier applied to delay after every failed attempt. 1 keeps the wait constant.

Returns

Promise<any>

Future<dynamic>

Any

Examples

javascript
// One call plus at most two retries
const data = await retry(() => fetchData('https://example.com'));

// Five attempts, waiting 100ms, 200ms, 400ms, 800ms
const result = await retry(() => unstableCall(), {
	times: 5,
	delay: 100,
	backoff: 2
});

// No retry at all
await retry(() => onlyOnce(), { times: 1 });
dart
// One call plus at most two retries
final data = await retry(() => unstableCall());

// Five attempts, waiting 100ms, 200ms, 400ms, 800ms
final result = await retry(() => unstableCall(), times: 5, delay: 100, backoff: 2);

// No retry at all
await retry(() => onlyOnce(), times: 1);
python
# One call plus at most two retries
data = retry(lambda: fetchData('https://example.com'))

# Five attempts, waiting 100ms, 200ms, 400ms, 800ms
result = retry(unstable_call, {
	'times': 5,
	'delay': 100,
	'backoff': 2
})

# No retry at all
retry(only_once, times=1)

Released under the MIT License