No more create promis
This commit is contained in:
parent
44947dc3a5
commit
adad2d5703
2 changed files with 1 additions and 27 deletions
|
|
@ -1,25 +0,0 @@
|
||||||
type ResolveFunction<T> = undefined extends T
|
|
||||||
? (value?: T) => unknown
|
|
||||||
: (value: T) => unknown;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A type-safe utility function to create a Promise with resolve and reject functions.
|
|
||||||
* @returns A tuple containing a Promise, a resolve function, and a reject function.
|
|
||||||
*/
|
|
||||||
export function createPromise<T = unknown>(): [
|
|
||||||
Promise<T>,
|
|
||||||
ResolveFunction<T>,
|
|
||||||
(error: unknown) => unknown
|
|
||||||
] {
|
|
||||||
let resolve: undefined | ResolveFunction<T> = undefined;
|
|
||||||
let reject: undefined | ((error: unknown) => unknown) = undefined;
|
|
||||||
|
|
||||||
const creationPromise = new Promise<T>(
|
|
||||||
(resolve_, reject_) =>
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
||||||
((resolve = resolve_ as ResolveFunction<T>), (reject = reject_))
|
|
||||||
);
|
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
||||||
return [creationPromise, resolve!, reject!];
|
|
||||||
}
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
import { createPromise } from "./create-promise";
|
|
||||||
import { sleep } from "./sleep";
|
import { sleep } from "./sleep";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -45,7 +44,7 @@ export function rateLimit<
|
||||||
newArgs = undefined;
|
newArgs = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const [promise, resolve] = createPromise();
|
const { promise, resolve } = Promise.withResolvers<void>();
|
||||||
running = promise;
|
running = promise;
|
||||||
sleep(
|
sleep(
|
||||||
typeof minIntervalMs === "function"
|
typeof minIntervalMs === "function"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue