调用一个方法继续之前的等待

export function collectWatchFn(callBack) {
  let status = '';
  let arrFn = [];
  if (status === 'ing') {
    return;
  }
  return function (...args) {
    return new Promise(async (resolve, reject) => {
      if (status === 'ing') {
        //@ts-ignore
        arrFn.push((flag) => {
          if (flag) {
            resolve(true);
          }
        });

        return;
      }
      status = 'ing';
      //@ts-ignore
      await callBack.call(this, ...args);
      status = '';
      if (arrFn.length) {
        arrFn.forEach((item) => {
          //@ts-ignore
          item(true);
        });
        arrFn = [];
      } else {
        resolve(true);
      }
    });
  };
}

  

posted @ 2023-09-12 10:03  无忧lv  阅读(1)  评论(0编辑  收藏  举报