defer
function
Defers invoking the func until the current call stack has cleared.
Installation
Import
import { defer } from '@tulx/utils';Source Code
Implementation
/**
* Defers invoking the func until the current call stack has cleared.
*
* @param func - The function to defer.
* @param args - The arguments to invoke func with.
* @returns Returns the timer id.
*
* @example
* ```ts
* defer((text: string) => console.log(text), 'deferred'); // Logs 'deferred' after one millisecond.
* ```
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function defer<T extends (...args: any[]) => any>(
func: T,
...args: Parameters<T>
): ReturnType<typeof setTimeout> {
return setTimeout(() => func(...args), 1);
}
Example
import { defer } from '@tulx/utils';
defer((text: string) => console.log(text), 'deferred'); // Logs 'deferred' after one millisecond.Related Functions
after
The opposite of before; this method creates a function that invokes func once it's called n or more times.
ary
Creates a function that invokes func, with up to n arguments, ignoring any additional arguments.
before
Creates a function that invokes func, with the this binding and arguments of the created function, while it's called less than n times.
bind
Creates a function that invokes func with the this binding of thisArg and partials prepended to the arguments it receives.
bindKey
Creates a function that invokes the method at object[key] with partials prepended to the arguments it receives.
curry
Creates a function that accepts arguments of func and either invokes func returning its result, or returns a function that accepts the remaining arguments.