21 lines
659 B
JavaScript
21 lines
659 B
JavaScript
|
|
//#region src/compat/function/defer.ts
|
||
|
|
/**
|
||
|
|
* Defers invoking the `func` until the current call stack has cleared. Any additional arguments are provided to func when it's invoked.
|
||
|
|
*
|
||
|
|
* @param {F} func The function to defer.
|
||
|
|
* @param {Parameters<F>} args The arguments to invoke `func` with.
|
||
|
|
* @returns {number} Returns the timer id.
|
||
|
|
*
|
||
|
|
* @example
|
||
|
|
* defer((text) => {
|
||
|
|
* console.log(text);
|
||
|
|
* }, 'deferred');
|
||
|
|
* // => Logs 'deferred' after the current call stack has cleared.
|
||
|
|
*/
|
||
|
|
function defer(func, ...args) {
|
||
|
|
if (typeof func !== "function") throw new TypeError("Expected a function");
|
||
|
|
return setTimeout(func, 1, ...args);
|
||
|
|
}
|
||
|
|
//#endregion
|
||
|
|
exports.defer = defer;
|