Home Docs Modules Common Common & Async Utilities Async helpers, debounce, throttle, retry, timeout, sleep, and execution flow controllers in sc-fns/common.
The sc-fns/common module provides essential asynchronous utilities and function execution modifiers.
ts
import { sleep, debounce, throttle, retry, timeout, once, noop, identity } from "sc-fns/common" ;
Returns a Promise that resolves after a specified number of milliseconds.
ts
function sleep ( ms : number ) : Promise < void >;
ts
async function run () {
console. log ( "Waiting..." );
await sleep ( 1500 ); // Pauses for 1.5 seconds
console. log ( "Resumed!" );
}
Creates a debounced version of a function that delays invocation until after wait milliseconds have elapsed since the last call. Includes .cancel(), .flush(), and .pending() controls.
ts
function debounce < T extends ( ... args : any []) => any >(
fn : T ,
wait : number ,
options ?: { leading ?: boolean ; trailing ?: boolean },
) : DebouncedFunction < T >;
ts
const handleSearch = debounce (( query : string ) => {
fetch ( `/api/search?q=${ query }` );
}, 300 );
// Call on each key stroke:
handleSearch ( "r" );
handleSearch ( "re" );
handleSearch ( "react" ); // Only this final call executes after 300ms!
// Cancel pending call:
handleSearch. cancel ();
Creates a throttled function that invokes at most once per every wait milliseconds.
ts
function throttle < T extends ( ... args : any []) => any >(
fn : T ,
wait : number ,
options ?: { leading ?: boolean ; trailing ?: boolean },
) : ThrottledFunction < T >;
ts
const handleScroll = throttle (() => {
console. log ( "Scroll position:" , window.scrollY);
}, 200 );
window. addEventListener ( "scroll" , handleScroll);
Retries an asynchronous operation upon rejection, supporting customizable attempt counts, delays, and exponential backoff.
ts
function retry < T >(
fn : () => Promise < T >,
options ?: {
retries ?: number ; // Default: 3
delay ?: number ; // Default: 1000ms
backoff ?: number ; // Default: 2 (multiplies delay after each attempt)
onRetry ?: ( error : unknown , attempt : number ) => void ;
},
) : Promise < T >;
ts
const data = await retry (() => fetch ( "https://api.unreliable.com/data" ). then (( res ) => res. json ()), {
retries: 3 ,
delay: 500 ,
backoff: 2 ,
onRetry : ( err , attempt ) => console. warn ( `Retry attempt #${ attempt }` , err),
});
Wraps a promise with a timeout deadline. If the promise does not resolve within ms milliseconds, it rejects with a timeout error.
ts
function timeout < T >( promise : Promise < T >, ms : number , error ?: Error | string ) : Promise < T >;
ts
try {
const result = await timeout ( fetchData (), 5000 , "Request timed out after 5s" );
} catch (err) {
console. error (err);
}
Small execution flow utilities:
ts
// Executes initialization logic exactly once, caching the result
const initializeDatabase = once (() => connectToDB ());
// Safe no-operation default
const onComplete = callback ?? noop;
// Identity pass-through
[ 1 , 2 , 3 ]. map (identity);
Last updated on October 25, 2026