defer

"Let go of the thoughts that don't make you strong." —Unknown

本文为 《lodash 源码阅读》 系列文章,后续内容会在 github 中发布,欢迎 star,gitbook 同步更新。

源码

/**
 * 推迟调用func,直到当前堆栈清理完毕。 调用时,任何附加的参数会传给func。
 *
 * @since 0.1.0
 * @category Function
 * @param {Function} func 要延迟的函数。
 * @param {...*} [args] 会在调用时传给 func 的参数。
 * @returns {number} 返回计时器 id。
 * @example
 *
 * defer(text => console.log(text), 'deferred')
 * // => Logs 'deferred' after one millisecond.
 */
function defer(func, ...args) {
  if (typeof func != 'function') {
    throw new TypeError('Expected a function');
  }
  return setTimeout(func, 1, ...args);
}

原理

通过 setTimeoutfunc 插入到 JavaScript 事件循环的消息队列中,使 func 会在当前堆栈清理完毕后被调用。

相关链接

Last updated

Was this helpful?