Skip to content

Commit

Permalink
feat: try constructor handle promise
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishqmanuja committed Nov 23, 2024
1 parent 55e0ac6 commit 96a7f4c
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/try.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ export type Try<A> = Success<A> | Failure<A>;
export type Unwrapped<A> = A extends Try<infer B> ? Unwrapped<B> : A;

export interface TryConstructor {
<A>(value: () => Promise<A>): Promise<A>;
<A>(value: () => A): Try<A>;

new <A>(value: () => Promise<A>): Promise<A>;
new <A>(value: () => A): Try<A>;

apply<A>(value: () => A): Try<A>;
Expand Down Expand Up @@ -172,8 +175,8 @@ const TryImplementation: Omit<TryConstructor, never> = {
try {
const v = value();

if (Object.prototype.toString.call(v) === "[object Promise]") {
return v as Promise<A>;
if (isPromise(v)) {
return v;
} else {
return Promise.resolve(v);
}
Expand All @@ -185,9 +188,18 @@ const TryImplementation: Omit<TryConstructor, never> = {
[Symbol.toStringTag]: "Try",
};

export const Try = Object.assign(function <A>(value: () => A): Try<A> {
return TryImplementation.apply(value);
}, TryImplementation) as TryConstructor;
export const Try = Object.assign(function <A>(
value: () => A | Promise<A>
): Try<A> | Promise<A> {
const v = TryImplementation.apply(value);

if (v.ok && isPromise(v.value)) {
return v.value;
} else {
return v as Try<A>;
}
},
TryImplementation) as TryConstructor;

/* Result Types */
type ResultOk<T> = {
Expand All @@ -201,3 +213,8 @@ type ResultError<E = unknown> = {
};

export type Result<T, E = unknown> = ResultOk<T> | ResultError<E>;

/* Helper */
function isPromise(value: unknown): value is Promise<unknown> {
return Object.prototype.toString.call(value) === "[object Promise]";
}

0 comments on commit 96a7f4c

Please sign in to comment.