neverthrow
Advanced tools
Comparing version 4.2.2 to 4.3.0
@@ -144,5 +144,7 @@ 'use strict'; | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
Ok.prototype.andThen = function (f) { | ||
return f(this.value); | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
Ok.prototype.orElse = function (_f) { | ||
@@ -190,6 +192,7 @@ return ok(this.value); | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
Err.prototype.andThen = function (_f) { | ||
return err(this.error); | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
Err.prototype.orElse = function (f) { | ||
@@ -269,2 +272,3 @@ return f(this.error); | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
ResultAsync.prototype.andThen = function (f) { | ||
@@ -279,2 +283,3 @@ return new ResultAsync(this._promise.then(function (res) { | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
ResultAsync.prototype.orElse = function (f) { | ||
@@ -281,0 +286,0 @@ var _this = this; |
@@ -0,1 +1,44 @@ | ||
declare class ResultAsync<T, E> implements PromiseLike<Result<T, E>> { | ||
private _promise; | ||
constructor(res: Promise<Result<T, E>>); | ||
static fromSafePromise<T, E>(promise: Promise<T>): ResultAsync<T, E>; | ||
static fromPromise<T, E>(promise: Promise<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>; | ||
map<A>(f: (t: T) => A | Promise<A>): ResultAsync<A, E>; | ||
mapErr<U>(f: (e: E) => U | Promise<U>): ResultAsync<T, U>; | ||
andThen<R extends Result<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferOkTypes<R>, InferErrTypes<R> | E>; | ||
andThen<R extends ResultAsync<unknown, unknown>>(f: (t: T) => R): ResultAsync<InferAsyncOkTypes<R>, InferAsyncErrTypes<R> | E>; | ||
andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F>; | ||
orElse<R extends Result<T, unknown>>(f: (e: E) => R): ResultAsync<T, InferErrTypes<R>>; | ||
orElse<R extends ResultAsync<T, unknown>>(f: (e: E) => R): ResultAsync<T, InferAsyncErrTypes<R>>; | ||
orElse<A>(f: (e: E) => Result<T, A> | ResultAsync<T, A>): ResultAsync<T, A>; | ||
match<A>(ok: (t: T) => A, _err: (e: E) => A): Promise<A>; | ||
unwrapOr<A>(t: A): Promise<T | A>; | ||
then<A, B>(successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>; | ||
} | ||
declare const okAsync: <T, E = never>(value: T) => ResultAsync<T, E>; | ||
declare const errAsync: <T = never, E = unknown>(err: E) => ResultAsync<T, E>; | ||
declare const fromPromise: typeof ResultAsync.fromPromise; | ||
declare const fromSafePromise: typeof ResultAsync.fromSafePromise; | ||
declare type ExtractOkTypes<T extends readonly Result<unknown, unknown>[]> = { | ||
[idx in keyof T]: T[idx] extends Result<infer U, unknown> ? U : never; | ||
}; | ||
declare type ExtractOkAsyncTypes<T extends readonly ResultAsync<unknown, unknown>[]> = { | ||
[idx in keyof T]: T[idx] extends ResultAsync<infer U, unknown> ? U : never; | ||
}; | ||
declare type ExtractErrTypes<T extends readonly Result<unknown, unknown>[]> = { | ||
[idx in keyof T]: T[idx] extends Result<unknown, infer E> ? E : never; | ||
}; | ||
declare type ExtractErrAsyncTypes<T extends readonly ResultAsync<unknown, unknown>[]> = { | ||
[idx in keyof T]: T[idx] extends ResultAsync<unknown, infer E> ? E : never; | ||
}; | ||
declare type InferOkTypes<R> = R extends Result<infer T, unknown> ? T : never; | ||
declare type InferErrTypes<R> = R extends Result<unknown, infer E> ? E : never; | ||
declare type InferAsyncOkTypes<R> = R extends ResultAsync<infer T, unknown> ? T : never; | ||
declare type InferAsyncErrTypes<R> = R extends ResultAsync<unknown, infer E> ? E : never; | ||
declare function combine<T extends readonly Result<unknown, unknown>[]>(resultList: T): Result<ExtractOkTypes<T>, ExtractErrTypes<T>[number]>; | ||
declare function combine<T extends readonly ResultAsync<unknown, unknown>[]>(asyncResultList: T): ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number]>; | ||
declare function combineWithAllErrors<T extends readonly Result<unknown, unknown>[]>(resultList: T): Result<ExtractOkTypes<T>, ExtractErrTypes<T>[number][]>; | ||
declare function combineWithAllErrors<T extends readonly ResultAsync<unknown, unknown>[]>(asyncResultList: T): ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number][]>; | ||
interface ErrorConfig { | ||
@@ -16,4 +59,4 @@ withStackTrace: boolean; | ||
declare type Result<T, E> = Ok<T, E> | Err<T, E>; | ||
declare const ok: <T, E>(value: T) => Ok<T, E>; | ||
declare const err: <T, E>(err: E) => Err<T, E>; | ||
declare const ok: <T, E = never>(value: T) => Ok<T, E>; | ||
declare const err: <T = never, E = unknown>(err: E) => Err<T, E>; | ||
interface IResult<T, E> { | ||
@@ -61,2 +104,3 @@ /** | ||
*/ | ||
andThen<R extends Result<unknown, unknown>>(f: (t: T) => R): Result<InferOkTypes<R>, InferErrTypes<R> | E>; | ||
andThen<U, F>(f: (t: T) => Result<U, F>): Result<U, E | F>; | ||
@@ -72,2 +116,3 @@ /** | ||
*/ | ||
orElse<R extends Result<unknown, unknown>>(f: (e: E) => R): Result<T, InferErrTypes<R>>; | ||
orElse<A>(f: (e: E) => Result<T, A>): Result<T, A>; | ||
@@ -97,3 +142,3 @@ /** | ||
*/ | ||
unwrapOr(v: T): T; | ||
unwrapOr<A>(v: A): T | A; | ||
/** | ||
@@ -139,7 +184,9 @@ * | ||
mapErr<U>(_f: (e: E) => U): Result<T, U>; | ||
andThen<R extends Result<unknown, unknown>>(f: (t: T) => R): Result<InferOkTypes<R>, InferErrTypes<R> | E>; | ||
andThen<U, F>(f: (t: T) => Result<U, F>): Result<U, E | F>; | ||
orElse<R extends Result<unknown, unknown>>(_f: (e: E) => R): Result<T, InferErrTypes<R>>; | ||
orElse<A>(_f: (e: E) => Result<T, A>): Result<T, A>; | ||
asyncAndThen<U, F>(f: (t: T) => ResultAsync<U, F>): ResultAsync<U, E | F>; | ||
asyncMap<U>(f: (t: T) => Promise<U>): ResultAsync<U, E>; | ||
unwrapOr(_v: T): T; | ||
unwrapOr<A>(_v: A): T | A; | ||
match<A>(ok: (t: T) => A, _err: (e: E) => A): A; | ||
@@ -156,7 +203,9 @@ _unsafeUnwrap(_?: ErrorConfig): T; | ||
mapErr<U>(f: (e: E) => U): Result<T, U>; | ||
andThen<R extends Result<unknown, unknown>>(_f: (t: T) => R): Result<InferOkTypes<R>, InferErrTypes<R> | E>; | ||
andThen<U, F>(_f: (t: T) => Result<U, F>): Result<U, E | F>; | ||
orElse<R extends Result<unknown, unknown>>(f: (e: E) => R): Result<T, InferErrTypes<R>>; | ||
orElse<A>(f: (e: E) => Result<T, A>): Result<T, A>; | ||
asyncAndThen<U, F>(_f: (t: T) => ResultAsync<U, F>): ResultAsync<U, E | F>; | ||
asyncMap<U>(_f: (t: T) => Promise<U>): ResultAsync<U, E>; | ||
unwrapOr(v: T): T; | ||
unwrapOr<A>(v: A): T | A; | ||
match<A>(_ok: (t: T) => A, err: (e: E) => A): A; | ||
@@ -168,37 +217,2 @@ _unsafeUnwrap(config?: ErrorConfig): T; | ||
declare class ResultAsync<T, E> implements PromiseLike<Result<T, E>> { | ||
private _promise; | ||
constructor(res: Promise<Result<T, E>>); | ||
static fromSafePromise<T, E>(promise: Promise<T>): ResultAsync<T, E>; | ||
static fromPromise<T, E>(promise: Promise<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>; | ||
map<A>(f: (t: T) => A | Promise<A>): ResultAsync<A, E>; | ||
mapErr<U>(f: (e: E) => U | Promise<U>): ResultAsync<T, U>; | ||
andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F>; | ||
orElse<A>(f: (e: E) => Result<T, A> | ResultAsync<T, A>): ResultAsync<T, A>; | ||
match<A>(ok: (t: T) => A, _err: (e: E) => A): Promise<A>; | ||
unwrapOr(t: T): Promise<T>; | ||
then<A, B>(successCallback?: (res: Result<T, E>) => A | PromiseLike<A>, failureCallback?: (reason: unknown) => B | PromiseLike<B>): PromiseLike<A | B>; | ||
} | ||
declare const okAsync: <T, E>(value: T) => ResultAsync<T, E>; | ||
declare const errAsync: <T, E>(err: E) => ResultAsync<T, E>; | ||
declare const fromPromise: typeof ResultAsync.fromPromise; | ||
declare const fromSafePromise: typeof ResultAsync.fromSafePromise; | ||
declare type ExtractOkTypes<T extends readonly Result<unknown, unknown>[]> = { | ||
[idx in keyof T]: T[idx] extends Result<infer U, unknown> ? U : never; | ||
}; | ||
declare type ExtractOkAsyncTypes<T extends readonly ResultAsync<unknown, unknown>[]> = { | ||
[idx in keyof T]: T[idx] extends ResultAsync<infer U, unknown> ? U : never; | ||
}; | ||
declare type ExtractErrTypes<T extends readonly Result<unknown, unknown>[]> = { | ||
[idx in keyof T]: T[idx] extends Result<unknown, infer E> ? E : never; | ||
}; | ||
declare type ExtractErrAsyncTypes<T extends readonly ResultAsync<unknown, unknown>[]> = { | ||
[idx in keyof T]: T[idx] extends ResultAsync<unknown, infer E> ? E : never; | ||
}; | ||
declare function combine<T extends readonly Result<unknown, unknown>[]>(resultList: T): Result<ExtractOkTypes<T>, ExtractErrTypes<T>[number]>; | ||
declare function combine<T extends readonly ResultAsync<unknown, unknown>[]>(asyncResultList: T): ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number]>; | ||
declare function combineWithAllErrors<T extends readonly Result<unknown, unknown>[]>(resultList: T): Result<ExtractOkTypes<T>, ExtractErrTypes<T>[number][]>; | ||
declare function combineWithAllErrors<T extends readonly ResultAsync<unknown, unknown>[]>(asyncResultList: T): ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number][]>; | ||
export { Err, Ok, Result, ResultAsync, combine, combineWithAllErrors, err, errAsync, fromPromise, fromSafePromise, fromThrowable, ok, okAsync }; |
@@ -142,5 +142,7 @@ /*! ***************************************************************************** | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
Ok.prototype.andThen = function (f) { | ||
return f(this.value); | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
Ok.prototype.orElse = function (_f) { | ||
@@ -188,6 +190,7 @@ return ok(this.value); | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
Err.prototype.andThen = function (_f) { | ||
return err(this.error); | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
Err.prototype.orElse = function (f) { | ||
@@ -267,2 +270,3 @@ return f(this.error); | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
ResultAsync.prototype.andThen = function (f) { | ||
@@ -277,2 +281,3 @@ return new ResultAsync(this._promise.then(function (res) { | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types | ||
ResultAsync.prototype.orElse = function (f) { | ||
@@ -279,0 +284,0 @@ var _this = this; |
{ | ||
"name": "neverthrow", | ||
"version": "4.2.2", | ||
"version": "4.3.0", | ||
"description": "Stop throwing errors, and instead return Results!", | ||
@@ -28,19 +28,19 @@ "main": "dist/index.cjs.js", | ||
"devDependencies": { | ||
"@babel/core": "7.14.0", | ||
"@babel/preset-env": "7.14.1", | ||
"@babel/preset-typescript": "7.13.0", | ||
"@types/jest": "26.0.22", | ||
"@types/node": "14.14.37", | ||
"@typescript-eslint/eslint-plugin": "4.12.0", | ||
"@typescript-eslint/parser": "4.12.0", | ||
"babel-jest": "26.6.3", | ||
"eslint": "7.26.0", | ||
"@babel/core": "7.15.0", | ||
"@babel/preset-env": "7.15.0", | ||
"@babel/preset-typescript": "7.15.0", | ||
"@types/jest": "26.0.24", | ||
"@types/node": "14.17.4", | ||
"@typescript-eslint/eslint-plugin": "4.28.1", | ||
"@typescript-eslint/parser": "4.28.1", | ||
"babel-jest": "27.0.6", | ||
"eslint": "7.30.0", | ||
"eslint-config-prettier": "7.1.0", | ||
"eslint-plugin-prettier": "3.4.0", | ||
"jest": "26.6.3", | ||
"jest": "27.0.6", | ||
"prettier": "2.2.1", | ||
"rollup": "2.47.0", | ||
"rollup": "2.52.7", | ||
"rollup-plugin-dts": "3.0.1", | ||
"rollup-plugin-typescript2": "0.29.0", | ||
"ts-jest": "26.4.4", | ||
"ts-jest": "27.0.3", | ||
"typescript": "4.2.4" | ||
@@ -47,0 +47,0 @@ }, |
152
README.md
@@ -7,3 +7,2 @@ # NeverThrow 🙅 | ||
## Description | ||
@@ -82,3 +81,2 @@ | ||
```typescript | ||
@@ -120,3 +118,3 @@ import { | ||
```typescript | ||
ok<T, E>(value: T): Ok<T, E> { ... } | ||
ok<T, E>(value: T): Ok<T, E> { ... } | ||
``` | ||
@@ -146,3 +144,3 @@ | ||
```typescript | ||
err<T, E>(err: E): Err<T, E> { ... } | ||
err<T, E>(error: E): Err<T, E> { ... } | ||
``` | ||
@@ -202,4 +200,5 @@ | ||
```typescript | ||
type MapFunc = <T, U>(f: T) => U | ||
map<U>(fn: MapFunc): Result<U, E> { ... } | ||
class Result<T, E> { | ||
map<U>(callback: (value: T) => U): Result<U, E> { ... } | ||
} | ||
``` | ||
@@ -240,4 +239,5 @@ | ||
```typescript | ||
type MapFunc = <E>(e: E) => F | ||
mapErr<U>(fn: MapFunc): Result<T, F> { ... } | ||
class Result<T, E> { | ||
mapErr<F>(callback: (error: E) => F): Result<T, F> { ... } | ||
} | ||
``` | ||
@@ -276,3 +276,5 @@ | ||
```typescript | ||
unwrapOr<T>(v: T): T { ... } | ||
class Result<T, E> { | ||
unwrapOr<T>(value: T): T { ... } | ||
} | ||
``` | ||
@@ -285,3 +287,3 @@ | ||
const multiply = (val: number): number => val * 2 | ||
const multiply = (value: number): number => value * 2 | ||
@@ -312,3 +314,5 @@ const unwrapped: number = myResult.map(multiply).unwrapOr(10) | ||
// then they will be merged into one type (`string`) | ||
andThen<U, F>(fn: (val: T) => Result<U, F>): Result<U, E | F> { ... } | ||
andThen<U, F>( | ||
callback: (value: T) => Result<U, F> | ||
): Result<U, E | F> { ... } | ||
} | ||
@@ -348,3 +352,3 @@ ``` | ||
// notNested is a Ok(1234) | ||
const notNested = nested.andThen(innerResult => innerResult) | ||
const notNested = nested.andThen((innerResult) => innerResult) | ||
``` | ||
@@ -366,3 +370,5 @@ | ||
class Result<T, E> { | ||
asyncAndThen<U, F>(fn: (val: T) => ResultAsync<U, F>): ResultAsync<U, E | F> { ... } | ||
asyncAndThen<U, F>( | ||
callback: (value: T) => ResultAsync<U, F> | ||
): ResultAsync<U, E | F> { ... } | ||
} | ||
@@ -382,4 +388,7 @@ ``` | ||
```typescript | ||
type ErrorCallback = <A>(e: E) => Result<T, A> | ||
orElse<A>(f: ErrorCallback<A>): Result<T, A> { ... } | ||
class Result<T, E> { | ||
orElse<A>( | ||
callback: (error: E) => Result<T, A> | ||
): Result<T, A> { ... } | ||
} | ||
``` | ||
@@ -397,3 +406,3 @@ | ||
const updatedQueryResult = dbQueryResult.orElse(dbError => | ||
const updatedQueryResult = dbQueryResult.orElse((dbError) => | ||
dbError === DatabaseError.NotFound | ||
@@ -415,3 +424,2 @@ ? ok('User does not exist') // error recovery branch: ok() must be called with a value of type string | ||
#### `Result.match` (method) | ||
@@ -426,6 +434,8 @@ | ||
```typescript | ||
match<A>( | ||
okFn: (t: T) => A, | ||
errFn: (e: E) => A | ||
): A => { ... } | ||
class Result<T, E> { | ||
match<A>( | ||
okCallback: (value: T) => A, | ||
errorCallback: (error: E) => A | ||
): A => { ... } | ||
} | ||
``` | ||
@@ -476,4 +486,7 @@ | ||
```typescript | ||
type MappingFunc = (t: T) => Promise<U> | ||
asyncMap<U>(fn: MappingFunc): ResultAsync<U, E> { ... } | ||
class Result<T, E> { | ||
asyncMap<U>( | ||
callback: (value: T) => Promise<U> | ||
): ResultAsync<U, E> { ... } | ||
} | ||
``` | ||
@@ -505,3 +518,3 @@ | ||
As such, when interfacing with third party libraries it's imperative that you | ||
wrap third-party code in try / catch blocks. | ||
wrap third-party code in try / catch blocks. | ||
@@ -521,3 +534,3 @@ This function will create a new function that returns an `Err` when the original | ||
type ParseError = { message: string } | ||
const toParseError = (): ParseError => ({message: "Parse Error" }) | ||
const toParseError = (): ParseError => ({ message: "Parse Error" }) | ||
@@ -558,3 +571,3 @@ const safeJsonParse = Result.fromThrowable(JSON.parse, toParseError) | ||
``` | ||
[⬆️ Back to top](#toc) | ||
@@ -571,3 +584,3 @@ | ||
```typescript | ||
errAsync<T, E>(err: E): ResultAsync<T, E> | ||
errAsync<T, E>(error: E): ResultAsync<T, E> | ||
``` | ||
@@ -601,3 +614,9 @@ | ||
```typescript | ||
fromPromise<U, E>(p: Promise<U>, f: (e: unknown) => E): ResultAsync<U, E> { ... } | ||
// fromPromise is a static class method | ||
// also available as a standalone function | ||
// import { fromPromise } from 'neverthrow' | ||
ResultAsync.fromPromise<T, E>( | ||
promise: Promise<T>, | ||
errorHandler: (unknownError: unknown) => E) | ||
): ResultAsync<T, E> { ... } | ||
``` | ||
@@ -613,3 +632,3 @@ | ||
const res = ResultAsync.fromPromise(insertIntoDb(myUser), () => new Error('Database error')) | ||
// res has a type of ResultAsync<User, Error> | ||
// `res` has a type of ResultAsync<User, Error> | ||
``` | ||
@@ -625,7 +644,11 @@ | ||
**Signature:** | ||
```typescript | ||
fromSafePromise<T, E>(p: Promise<T>): ResultAsync<T, E> { ... } | ||
// fromPromise is a static class method | ||
// also available as a standalone function | ||
// import { fromPromise } from 'neverthrow' | ||
ResultAsync.fromSafePromise<T, E>( | ||
promise: Promise<T> | ||
): ResultAsync<T, E> { ... } | ||
``` | ||
@@ -640,7 +663,7 @@ | ||
// Adopted from https://github.com/parlez-vous/server/blob/2496bacf55a2acbebc30631b5562f34272794d76/src/routes/common/signup.ts | ||
export const slowDown = <T>(ms: number) => (val: T) => | ||
export const slowDown = <T>(ms: number) => (value: T) => | ||
ResultAsync.fromSafePromise<T, RouteError>( | ||
new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(val) | ||
resolve(value) | ||
}, ms) | ||
@@ -676,4 +699,7 @@ }) | ||
```typescript | ||
type MapFunc = <T>(f: T | Promise<T>) => U | ||
map<U>(fn: MapFunc): ResultAsync<U, E> { ... } | ||
class ResultAsync<T, E> { | ||
map<U>( | ||
callback: (value: T) => U | Promise<U> | ||
): ResultAsync<U, E> { ... } | ||
} | ||
``` | ||
@@ -720,4 +746,7 @@ | ||
```typescript | ||
type MapFunc = <E>(e: E) => F | Promise<F> | ||
mapErr<U>(fn: MapFunc): ResultAsync<T, F> { ... } | ||
class ResultAsync<T, E> { | ||
mapErr<F>( | ||
callback: (error: E) => F | Promise<F> | ||
): ResultAsync<T, F> { ... } | ||
} | ||
``` | ||
@@ -733,6 +762,6 @@ | ||
// Let's say we need to low-level errors from findUsersIn to be more readable | ||
const usersInCanada = findUsersIn("Canada").mapErr((e: Error) => { | ||
const usersInCanada = findUsersIn("Canada").mapErr((error: Error) => { | ||
// The only error we want to pass to the user is "Unknown country" | ||
if(e.message === "Unknown country"){ | ||
return e.message | ||
if(error.message === "Unknown country"){ | ||
return error.message | ||
} | ||
@@ -771,3 +800,5 @@ // All other errors will be labelled as a system error | ||
```typescript | ||
unwrapOr<T>(v: T): Promise<T> { ... } | ||
class ResultAsync<T, E> { | ||
unwrapOr<T>(value: T): Promise<T> { ... } | ||
} | ||
``` | ||
@@ -799,10 +830,10 @@ | ||
```typescript | ||
type AndThenFunc = (t: T) => ResultAsync<U, E> | Result<U, E> | ||
andThen<U>(f: AndThenFunc): ResultAsync<U, E> { ... } | ||
// Note that the latest version (v4.1.0-beta) lets you return distinct errors as well. | ||
// If the error types (E and F) are the same (like `string | string`) | ||
// then they will be merged into one type (`string`) | ||
class ResultAsync<T, E> { | ||
// Note that the latest version (v4.1.0-beta) lets you return distinct errors as well. | ||
// If the error types (E and F) are the same (like `string | string`) | ||
// then they will be merged into one type (`string`) | ||
andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F> { ... } | ||
andThen<U, F>( | ||
callback: (value: T) => Result<U, F> | ResultAsync<U, F> | ||
): ResultAsync<U, E | F> { ... } | ||
} | ||
@@ -842,5 +873,4 @@ ``` | ||
--- | ||
--- | ||
#### `ResultAsync.orElse` (method) | ||
@@ -853,4 +883,7 @@ | ||
```typescript | ||
type ErrorCallback = <A>(e: E) => Result<T, A> | ResultAsync<T, A> | ||
orElse<A>(f: ErrorCallback<A>): ResultAsync<T, A> { ... } | ||
class ResultAsync<T, E> { | ||
orElse<A>( | ||
callback: (error: E) => Result<T, A> | ResultAsync<T, A> | ||
): ResultAsync<T, A> { ... } | ||
} | ||
``` | ||
@@ -871,6 +904,8 @@ | ||
```typescript | ||
match<A>( | ||
okFn: (t: T) => A, | ||
errFn: (e: E) => A | ||
): Promise<A> => { ... } | ||
class ResultAsync<T, E> { | ||
match<A>( | ||
okCallback: (value: T) => A, | ||
errorCallback: (error: E) => A | ||
): Promise<A> => { ... } | ||
} | ||
``` | ||
@@ -894,3 +929,3 @@ | ||
(user: User) => `User ${user.name} has been successfully created`, | ||
(e: Error) => `User could not be created because ${e.message}` | ||
(error: Error) => `User could not be created because ${error.message}` | ||
) | ||
@@ -1017,3 +1052,3 @@ | ||
`Result` instances have two unsafe methods, aptly called `_unsafeUnwrap` and `_unsafeUnwrapErr` which **should only be used in a test environment**. | ||
`Result` instances have two unsafe methods, aptly called `_unsafeUnwrap` and `_unsafeUnwrapErr` which **should only be used in a test environment**. | ||
@@ -1050,6 +1085,4 @@ `_unsafeUnwrap` takes a `Result<T, E>` and returns a `T` when the result is an `Ok`, otherwise it throws a custom object. | ||
--- | ||
If you find this package useful, please consider [sponsoring me](https://github.com/sponsors/supermacro/) or simply [buying me a coffee](https://ko-fi.com/gdelgado)! | ||
@@ -1059,3 +1092,2 @@ | ||
## A note on the Package Name | ||
@@ -1062,0 +1094,0 @@ |
Sorry, the diff of this file is not supported yet
74354
954
1064