Skip to content

Commit

Permalink
chore: add Future utility types
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr Schemelev committed Dec 24, 2024
1 parent 348b298 commit 0f4c8a3
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions metautil.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,31 @@ export class EventEmitter {
}

export function once(emitter: EventEmitter, name: string): Promise<unknown>;

// Submodule: Future

type Resolve<T = unknown> = (value: T) => void;
type Reject = (reason?: Error | unknown) => void;
type Executor<T = unknown> = (resolve: Resolve<T>, reject?: Reject) => void;

interface Thenable<T = unknown> {
then(resolve: Resolve<T>, reject: Reject): unknown;
}

export class Future<T = unknown> {
static of<U = unknown>(value: U): Future<U>;
constructor(executor: Executor<T>);
chain<U = T>(fn: (value: T) => Future<U>): Future<U>;
map<U = T>(fn: (value: T) => U): Future<U>;
fork(successed: Resolve<T>, failed?: Reject): void;
toPromise(): Promise<T>;
toThenable(): Thenable<T>;
toCallbackLast(callback: Function): CallbackLastFunction;
}

type CallbackLastArgs = [...args: Array<unknown>, callback: Function];
type CallbackLastFunction = (...args: CallbackLastArgs) => unknown;

export function futurify(
fn: CallbackLastFunction,
): (...args: CallbackLastArgs) => Future;

0 comments on commit 0f4c8a3

Please sign in to comment.