Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Future utility types #274

Open
wants to merge 2 commits into
base: future
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => unknown;

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think about returning value

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need more use cases (tests) to think about contract improvements

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;