This website requires JavaScript.

coding

JS, 1 posts

实用的代码整理

js中防抖、节流函数如何获取原函数的返回值?

利用回调函数和Promise,参考ahooks源码3.0(2.0不支持)。

防抖/节流模式下,runAsync 可以返回正常 Promise。

  const debounceCallback = debounce(
    (callback) => {
      callback();
    },
    1000,
    { leading: true, trailing: true },
  );
  const _originRunAsync = (params?: any) => {
    return new Promise((resolve, reject) => {
      // @ts-ignore
      debounceCallback?.(() => fetchReceive(params).then(resolve).catch(reject));
    });
  };

查询问题:

ahooks-useRequest 探究