Skip to content

Commit

Permalink
Start async option docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bkiac committed Nov 27, 2024
1 parent 424268e commit 0ad46e6
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/async_option.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* This module contains the `AsyncOption` class, which is a promise that resolves to an `Option`.
* @module
*/

import { AsyncResult } from "./async_result.ts";
import { None, type Option, type OptionMatch, type OptionMatchAsync, Some } from "./option.ts";

Expand All @@ -7,8 +12,15 @@ import { None, type Option, type OptionMatch, type OptionMatchAsync, Some } from
* This class is useful for chaining multiple asynchronous operations that return an `Option`.
*/
export class AsyncOption<T> implements PromiseLike<Option<T>> {
/**
* The promise that resolves to an `Option`.
*/
public readonly promise: Promise<Option<T>> | PromiseLike<Option<T>> | AsyncOption<T>;

/**
* Creates a new `AsyncOption`.
* @param promise - The promise that resolves to an `Option`.
*/
public constructor(promise: Promise<Option<T>> | PromiseLike<Option<T>> | AsyncOption<T>) {
this.promise = promise;
}
Expand All @@ -17,10 +29,16 @@ export class AsyncOption<T> implements PromiseLike<Option<T>> {
return "AsyncOption";
}

/**
* Converts the `AsyncOption` to a JSON object.
*/
public toJSON(): { AsyncOption: Promise<Option<T>> | PromiseLike<Option<T>> | AsyncOption<T> } {
return { AsyncOption: this.promise };
}

/**
* Converts the `AsyncOption` to a string.
*/
public toString(): string {
return `AsyncOption(${this.promise.toString()})`;
}
Expand Down

0 comments on commit 0ad46e6

Please sign in to comment.