debounce
code:ts
export function debouncePromise<Fn extends (...args: any[]) => any>(
fn: Fn,
ms: number,
abortValue: any = undefined,
) {
let cancel = () => {};
return (...args: Parameters<Fn>): Promise<ReturnType<Fn>> => {
cancel();
return new Promise((resolve, reject) => {
const timer = setTimeout(() => resolve(fn(...args)), ms);
cancel = () => {
clearTimeout(timer);
if (abortValue !== undefined) {
reject(abortValue);
}
};
});
};
}