useContinuousRetry

Continuously retry a function until it succeeds.

https://www.shaktools.com/shak-hooks
Utility

API Reference

Installation

terminal
pnpm add @shak-hooks/usehooks
import.ts
import { useContinuousRetry } from "@shak-hooks/usehooks";
Source: packages/react/src/useContinuousRetry.ts

Signature

signature.ts
export function useContinuousRetry(callback: () => boolean | Promise<boolean>, interval: number = 100, options: {
maxRetries?: number;
} = {})

Parameters

NameTypeOptionalDefault
callback() => boolean | Promise<boolean>No-
intervalnumberYes100
options{ maxRetries?: number; }Yes{}

Returns

unknown

Implementation

use-continuous-retry.ts
1import { useState, useEffect, useRef } from "react";
2
3export function useContinuousRetry(
4 callback: () => boolean,
5 interval: number = 100,
6 maxRetries: number = 10
7) {
8 const [hasSucceeded, setHasSucceeded] = useState(false);
9 const retries = useRef(0);
10
11 useEffect(() => {
12 const id = setInterval(() => {
13 if (callback()) {
14 setHasSucceeded(true);
15 clearInterval(id);
16 } else if (retries.current >= maxRetries) {
17 clearInterval(id);
18 }
19 retries.current += 1;
20 }, interval);
21
22 return () => clearInterval(id);
23 }, [callback, interval, maxRetries]);
24
25 return hasSucceeded;
26}

Advertisement

Google Ads

Usage

example.tsx
1import { useContinuousRetry } from "@shak-hooks/usehooks";
2
3const result = useContinuousRetry(false, undefined, undefined);
4// result: unknown
5// Use values directly (React state).

Let‘s do great work together

Empowering creators with free, high-performance AI, SEO, and developer tools. Join thousands of users optimizing their workflow with Shak-Tools.

Tools10+ Free
UsersGlobal

Stay in the loop

Join our newsletter for the latest AI tools and updates.

ShakTools
© 2025 Shaktools. All Rights Reserved.Privacy Policy