Skip to content
Closed
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
32 changes: 29 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
// blatantly stolen from fireship at https://www.youtube.com/watch?v=ITogH7lJTyE

export class OK<T> extends Array<T | null> {
public readonly [0]: null = null;
public readonly [1]: T;
public error: null = null;
public data: T;

constructor(data: T) {
super(2);
this[1] = data;
this.data = data;
}
}

export class ERR<E> extends Array<E | null> {
public readonly [0]: E;
public readonly [1]: null = null;
public data: null = null;
public error: E;

constructor(error: E) {
super(2);
this[0] = error;
this.error = error;
}
}

export const trytm = async <T>(
promise: Promise<T>,
): Promise<[T, null] | [null, Error]> => {
): Promise<OK<T> | ERR<Error>> => {
try {
const data = await promise;
return [data, null];
return new OK(data);
} catch (throwable) {
if (throwable instanceof Error) return [null, throwable];
if (throwable instanceof Error) return new ERR(throwable);

throw throwable;
}
Expand Down