diff --git a/packages/pure/package.json b/packages/pure/package.json index 4ea6bcb..dbb681b 100644 --- a/packages/pure/package.json +++ b/packages/pure/package.json @@ -1,6 +1,6 @@ { "name": "@pistonite/pure", - "version": "0.26.7", + "version": "0.26.8", "type": "module", "description": "Pure TypeScript libraries for my projects", "homepage": "https://github.com/Pistonite/pure", diff --git a/packages/pure/src/sync/index.ts b/packages/pure/src/sync/index.ts index 30322af..e5c658d 100644 --- a/packages/pure/src/sync/index.ts +++ b/packages/pure/src/sync/index.ts @@ -14,8 +14,9 @@ export { latest, type LatestConstructor, type UpdateArgsFn } from "./latest.ts"; export { debounce, type DebounceConstructor } from "./debounce.ts"; export { batch, type BatchConstructor } from "./batch.ts"; export { once, type OnceConstructor } from "./once.ts"; +export { makePromise, type PromiseHandle } from "./util.ts"; -// types +// helper types export type { AnyFn, AwaitRet } from "./util.ts"; // unstable diff --git a/packages/pure/src/sync/util.ts b/packages/pure/src/sync/util.ts index dede0e9..3a1a57d 100644 --- a/packages/pure/src/sync/util.ts +++ b/packages/pure/src/sync/util.ts @@ -1,23 +1,28 @@ -export const makePromise = (): { - promise: Promise; - resolve: (value: T | PromiseLike) => void; - reject: (reason?: unknown) => void; -} => { - let resolve; - let reject; +/** + * Make a {@link PromiseHandle} with the promise object separate from + * its resolve and reject methods + */ +export const makePromise = (): PromiseHandle => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + let resolve: any; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + let reject: any; const promise = new Promise((res, rej) => { resolve = res; reject = rej; }); - if (!resolve || !reject) { - throw new Error( - "Promise callbacks not set. This is a bug in the JS engine!", - ); - } return { promise, resolve, reject }; }; -export type PromiseHandle = ReturnType>; +/** + * A handle of the promise that breaks down the promise object + * and its resolve and reject functions + */ +export type PromiseHandle = { + promise: Promise; + resolve: (value: T | PromiseLike) => void; + reject: (reason?: unknown) => void; +}; /** Shorthand for Awaited> */ // eslint-disable-next-line @typescript-eslint/no-explicit-any