diff --git a/src/index.ts b/src/index.ts index 356d112..c2fc8e9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,39 @@ // blatantly stolen from fireship at https://www.youtube.com/watch?v=ITogH7lJTyE +export class OK extends Array { + 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 extends Array { + 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 ( promise: Promise, -): Promise<[T, null] | [null, Error]> => { +): Promise | ERR> => { 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; }