Skip to content
Open
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
36 changes: 33 additions & 3 deletions lib/result.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/**
* @ignore
* A value representing either a successful outcome or an error.
*
* `Result<T>` is used in APIs when you want to preserve both successes and
* failures instead of short-circuiting on the first error.
*
* A successful result has the shape `{ ok: true, value }` and a failed result
* has the shape `{ ok: false, error }`.
*
* @since 4.1
*/
export type Result<T> = {
readonly ok: true;
Expand All @@ -10,7 +18,18 @@ export type Result<T> = {
};

/**
* @ignore
* Construct a successful {@link Result}.
*
* ### Example
*
* ```javascript
* import { Ok } from 'effection';
*
* let result = Ok("hello");
* // { ok: true, value: "hello" }
* ```
*
* @since 4.1
*/
export function Ok(): Result<void>;
export function Ok<T>(value: T): Result<T>;
Expand All @@ -22,7 +41,18 @@ export function Ok<T>(value?: T): Result<T | undefined> {
}

/**
* @ignore
* Construct a failed {@link Result}.
*
* ### Example
*
* ```javascript
* import { Err } from 'effection';
*
* let result = Err(new Error("oh no"));
* // { ok: false, error: Error("oh no") }
* ```
*
* @since 4.1
*/
export const Err = <T>(error: Error): Result<T> => ({ ok: false, error });

Expand Down