图片说明

//简单版
function debounce(fn) {
  let isExecuted = false;
  return function () {
    if (!isExecuted) {
      isExecuted = true;
      new Promise((resolve, reject) => {
        fn();
        resolve();
      }).then(() => {
        isExecuted = false;
      })
    }
  }
}
//考虑fn带参数及fn可能有返回值情况
function debounce(fn) {
  let isExecuted = false;
  return function (...args) {
    if (!isExecuted) {
      isExecuted = true;
      let res;
      new Promise((resolve, reject) => {
        res = fn.apply(null, args);
        resolve();
      }).then(() => {
        isExecuted = false;
      })
      return res;
    }
  }
}