Skip to content

Commit

Permalink
Add reject type to task interface
Browse files Browse the repository at this point in the history
de1c6429d047fdbb0b1052e77b5ac1ecdfa9ad72
  • Loading branch information
sabio committed Mar 28, 2024
1 parent 233e1f6 commit d4e62d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/utils/async/__test__/task.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('Tasks', () => {
resolve(1);
});
const chain = taskChain((result?: number) =>
task((reject, resolve) => {
task<number>((reject, resolve) => {
resolve(result! + 1);
}),
);
Expand Down
26 changes: 13 additions & 13 deletions src/utils/async/task.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { cont, curry2 } from '../function';
import { cForEach } from '../array';

export type ForkInterface<ResolveType> = (
reject: (a: any) => void,
export type ForkInterface<ResolveType, RejectType = unknown> = (
reject: (a: RejectType) => void,
resolve: (a: ResolveType) => void,
) => void;

export type TaskInterface<ResolveType> = <R>(
fn: (forks: ForkInterface<ResolveType>) => R,
export type TaskInterface<ResolveType, RejectType = unknown> = <R>(
fn: (forks: ForkInterface<ResolveType, RejectType>) => R,
) => R;

export const task = cont as <ResolveType = any>(
export const task = cont as <ResolveType, RejectType = unknown>(
// eslint-disable-next-line no-use-before-define
fork: ForkInterface<ResolveType>,
fork: ForkInterface<ResolveType, RejectType>,
// eslint-disable-next-line no-use-before-define
) => TaskInterface<ResolveType>;
) => TaskInterface<ResolveType, RejectType>;

export const taskFork =
<In>(reject: (a: In) => void, resolve: (a: In) => void) =>
(fork: ForkInterface<In>) =>
<In, E = unknown>(reject: (a: E) => void, resolve: (a: In) => void) =>
(fork: ForkInterface<In, E>) =>
fork(reject, resolve);

export const taskMap = curry2(
Expand All @@ -35,14 +35,14 @@ export const taskMap = curry2(
);

export const taskChain = curry2(
<ChainIn, ChainOut>(
fn: (chainResult: ChainIn) => TaskInterface<ChainOut>,
<ChainIn, ChainOut, E = unknown>(
fn: (chainResult: ChainIn) => TaskInterface<ChainOut, E>,
fork: ForkInterface<ChainIn>,
) =>
task<ChainOut>((reject, resolve) =>
fork(reject, (result) => {
try {
fn(result)(taskFork<ChainOut>(reject, resolve));
fn(result)(taskFork<ChainOut, E>(reject, resolve));
} catch (e) {
reject(e);
}
Expand Down Expand Up @@ -102,7 +102,7 @@ export const taskAll = <T>(tasks: TaskInterface<T>[]): TaskInterface<T[]> => {
};

export const fromPromise = <T>(promise: Promise<T>) =>
task((reject, resolve) => {
task<T>((reject, resolve) => {
promise.then(resolve, reject);
});

Expand Down

0 comments on commit d4e62d0

Please sign in to comment.