Skip to content

Commit

Permalink
feat: add /promises module (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatiusmb authored Oct 1, 2024
1 parent 4b7031a commit 74f715e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions workspace/mauss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
"types": "./index.d.ts",
"default": "./src/math/index.js"
},
"./promises": {
"types": "./index.d.ts",
"default": "./src/promises/index.js"
},
"./random": {
"types": "./index.d.ts",
"default": "./src/random/index.js"
Expand Down
19 changes: 19 additions & 0 deletions workspace/mauss/src/promises/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { AnyFunction } from '../typings/helpers.js';

const DURATION = 300;

export function debounce<F extends AnyFunction>(fn: F, time = DURATION) {
let timeout: NodeJS.Timeout;
type Returned = Promise<ReturnType<F>>;
return async <A extends Parameters<F>>(...args: A): Returned => {
if (timeout) clearTimeout(timeout);
await new Promise((fulfil) => {
timeout = setTimeout(fulfil, time);
});
return fn(...args);
};
}

export async function pause(ms: number): Promise<string> {
return new Promise((fulfil) => setTimeout(fulfil, ms));
}

0 comments on commit 74f715e

Please sign in to comment.