33 lines
757 B
TypeScript
33 lines
757 B
TypeScript
|
export function throttle(func: () => void, limit: number) {
|
||
|
let timer: any = null;
|
||
|
return function () {
|
||
|
if (!timer) {
|
||
|
timer = setTimeout(() => {
|
||
|
func();
|
||
|
timer = null;
|
||
|
}, limit);
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
// race for promises returns first promise that resolves
|
||
|
export function race<T>(promises: Promise<T>[]): Promise<T> {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
for (const p of promises) {
|
||
|
p.then(resolve, reject);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
export function timeout(ms: number): Promise<never> {
|
||
|
return new Promise((_resolve, reject) =>
|
||
|
setTimeout(() => {
|
||
|
reject(new Error("timeout"));
|
||
|
}, ms)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
export function sleep(ms: number): Promise<void> {
|
||
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||
|
}
|