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

Need tests, docs, and typings for Future #266

Open
tshemsedinov opened this issue Nov 29, 2024 · 8 comments · May be fixed by #274
Open

Need tests, docs, and typings for Future #266

tshemsedinov opened this issue Nov 29, 2024 · 8 comments · May be fixed by #274
Assignees

Comments

@tshemsedinov
Copy link
Member

We have implementation moved from https://github.com/HowProgrammingWorks/Future but need tests, docs, and typings as well.

Refs: #260

@c-0d3-r
Copy link

c-0d3-r commented Dec 3, 2024

@tshemsedinov Can I take task for adding typings?

@axbuglak
Copy link
Contributor

axbuglak commented Dec 8, 2024

I can also help with writing tests and documentation

@tshemsedinov
Copy link
Member Author

Implementation: @c-0d3-r
Review: @axbuglak @tshemsedinov

@c-0d3-r
Copy link

c-0d3-r commented Dec 11, 2024

type FutureExecutor = (resolve: Function, reject?: Function) => void;

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

export class Future {
  // #1
  constructor(executor: FutureExecutor);
  // #2
  constructor(executor: Function);
  static of(value: unknown): Future;
  chain(fn: Function): Future;
  map(fn: Function): Future;
  fork(successed: Function, failed?: Function): void;
  toPromise(): Promise<unknown>;
  toThenable(): Thenable<unknown>;
}

// #1
type CallbackLastParams = [...params: Array<unknown>, callback: Function];
export function futurify(fn: Function): (...args: CallbackLastParams) => Future;

// #2
export function futurify(fn: Function): (...args: Array<unknown>) => Future;

@c-0d3-r
Copy link

c-0d3-r commented Dec 11, 2024

@tshemsedinov @axbuglak

with generics

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): void;
}

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>;
}

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

export function futurify(
  fn: CallbackLastFn,
): (...args: CallbackLastParams) => Future;

@axbuglak
Copy link
Contributor

axbuglak commented Dec 11, 2024

type FutureExecutor = (resolve: Function, reject?: Function) => void;

export class Future {
  // #1
  constructor(executor: FutureExecutor);
  // #2
  constructor(executor: Function);
  static of(value: unknown): Future;
  chain(fn: Function): Future;
  map(fn: Function): Future;
  fork(successed: Function, failed?: Function): void;
  toPromise(): Promise<unknown>;
  toThenable(): Promise<unknown>;
}

// #1
type CallbackLastParams = [...params: Array<unknown>, callback: Function];
export function futurify(fn: Function): (...args: CallbackLastParams) => Future;

// #2
export function futurify(fn: Function): (...args: Array<unknown>) => Future;

The first variant looks good, but can you please open a PR for it so i wil be able to edit something. For example toThenable method returns not just a Promise, but an object { then }

And would you like to write a documentation and tests too? Or may I take it?

@c-0d3-r
Copy link

c-0d3-r commented Dec 12, 2024

@axbuglak Yes, I will open a PR and adjust typings for toThenable as well. As you mentioned you can take writing docs and tests - no problems

@tshemsedinov
Copy link
Member Author

interface Thenable<T = unknown> {
  then(resolve: Resolve<T>, reject: Reject): unknown;
}
toCallbackLast(): CallbackLastFn;
  1. Rename CallbackLastFn to CallbackLastFunction

@c-0d3-r c-0d3-r linked a pull request Dec 24, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants