defer()
defer<
Args>(func, ...args):Timeout
Defers invoking the function until the current call stack has cleared.
DEPRECATED
Use setTimeout(fn, 0) directly instead.
Type Parametersβ
Args: Args extends unknown[]β
Parametersβ
func: (...args) => voidβ
The function to defer.
args: ...Argsβ
The arguments to invoke the function with.
Returns: Timeoutβ
The timer id.
See Alsoβ
Sinceβ
2.0.0
Also known asβ
defer (Lodash, es-toolkit) Β· β (Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)
Exampleβ
// β Deprecated approach
defer(console.log, 'deferred');
// β
Recommended approach
setTimeout(() => console.log('deferred'), 0);
How it works?β
Defers invoking func until the current call stack clears.
Deprecated: Use setTimeout(fn, 0) or queueMicrotask() directly.
Native Equivalentβ
// β defer(fn)
// β
setTimeout(fn, 0)
// β
queueMicrotask(fn)
Use Casesβ
Defer execution πβ
Execute function after current call stack.
queueMicrotask(() => {
console.log("Deferred");
});
Defer to next tickβ
Schedule for next event loop iteration.
setTimeout(() => {
updateUI();
}, 0);
Non-blocking operationβ
Allow current execution to complete first.
Promise.resolve().then(() => {
heavyComputation();
});