mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Consolidate delay modules.
This commit is contained in:
@@ -1,18 +0,0 @@
|
|||||||
/**
|
|
||||||
* The base implementation of `delay` and `defer` which accepts `args`
|
|
||||||
* to provide to `func`.
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {Function} func The function to delay.
|
|
||||||
* @param {number} wait The number of milliseconds to delay invocation.
|
|
||||||
* @param {Array} args The arguments to provide to `func`.
|
|
||||||
* @returns {number|Object} Returns the timer id or timeout object.
|
|
||||||
*/
|
|
||||||
function baseDelay(func, wait, args) {
|
|
||||||
if (typeof func != 'function') {
|
|
||||||
throw new TypeError('Expected a function');
|
|
||||||
}
|
|
||||||
return setTimeout(() => func(...args), wait);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default baseDelay;
|
|
||||||
7
defer.js
7
defer.js
@@ -1,5 +1,3 @@
|
|||||||
import baseDelay from './.internal/baseDelay.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defers invoking the `func` until the current call stack has cleared. Any
|
* Defers invoking the `func` until the current call stack has cleared. Any
|
||||||
* additional arguments are provided to `func` when it's invoked.
|
* additional arguments are provided to `func` when it's invoked.
|
||||||
@@ -15,7 +13,10 @@ import baseDelay from './.internal/baseDelay.js';
|
|||||||
* // => Logs 'deferred' after one millisecond.
|
* // => Logs 'deferred' after one millisecond.
|
||||||
*/
|
*/
|
||||||
function defer(func, ...args) {
|
function defer(func, ...args) {
|
||||||
return baseDelay(func, 1, args);
|
if (typeof func != 'function') {
|
||||||
|
throw new TypeError('Expected a function');
|
||||||
|
}
|
||||||
|
return setTimeout(() => func(...args), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defer;
|
export default defer;
|
||||||
|
|||||||
6
delay.js
6
delay.js
@@ -1,4 +1,3 @@
|
|||||||
import baseDelay from './.internal/baseDelay.js';
|
|
||||||
import toNumber from './toNumber.js';
|
import toNumber from './toNumber.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,7 +16,10 @@ import toNumber from './toNumber.js';
|
|||||||
* // => Logs 'later' after one second.
|
* // => Logs 'later' after one second.
|
||||||
*/
|
*/
|
||||||
function delay(func, wait, ...args) {
|
function delay(func, wait, ...args) {
|
||||||
return baseDelay(func, toNumber(wait) || 0, args);
|
if (typeof func != 'function') {
|
||||||
|
throw new TypeError('Expected a function');
|
||||||
|
}
|
||||||
|
return setTimeout(() => func(...args), toNumber(wait) || 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default delay;
|
export default delay;
|
||||||
|
|||||||
Reference in New Issue
Block a user