Skip to content

Commit

Permalink
docs: update README
Browse files Browse the repository at this point in the history
  • Loading branch information
yifanwww committed Sep 16, 2023
1 parent 09bfcef commit 16edf90
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ The Rust-like `Result` implements the following methods:
| orElse | [or_else] |
| transpose | [transpose] |

Unlike Rust, JavaScript doesn't have the 'Ownership' feature, so some API like `as_ref` is not necessary. These implementations are not implemented:
Unlike Rust, JavaScript doesn't have the 'Ownership' feature, so some API like `as_ref` are not necessary. These implementations are not implemented:

```md
<!-- implementations -->
Expand Down Expand Up @@ -129,6 +129,44 @@ There is a [proposal] (stage 2) that introduces `Record` and `Tuple` which are c

[proposal]: https://github.com/tc39/proposal-record-tuple

## More Helper Functions
### resultify

Takes a function and returns a version that returns results asynchronously.

```ts
import fs from 'node:fs/promises';

const copyFile1 = resultify(fs.copyFile);
const copyFile2 = resultify<Error>()(fs.copyFile);
```

### resultify.sync

Takes a function and returns a version that returns results synchronously.

```ts
/**
* @throws {Error} Some error messages
*/
function fn(): string {
// do something
}

const fn1 = resultify.sync(fn);
const fn1 = resultify.sync<Error>()(fn);
```

In the context where async functions are not allowed, you can use this function to resultify the sync function.

### resultify.promise

Takes a promise and returns a new promise that contains a result.

```ts
const result = await resultify.promise(promise);
```

## License

MIT
30 changes: 25 additions & 5 deletions src/resultify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,34 @@ type CurriedResultifySync<E> = <T, Args extends unknown[]>(
) => (...args: Args) => Result<NoVoid<T>, E>;

/**
* Takes a function and returns a version that returns results.
* Takes a function and returns a version that returns results synchronously.
*
* If you need the error value and want to specify its type, please use another overloaded function.
* Examples:
* ```ts
* function fn(): string {
* // throws error if failed
* }
* const fn1 = resultify.sync(fn);
* ```
*
* In the context where async functions are not allowed, you can use this function to resultify the sync function.
* If you want to resultify an async function, please use `resultify` instead.
*
* If you need the error value and want to specify its type, please use another overloaded function.
*/
function resultifySync<T, E, Args extends unknown[]>(fn: (...args: Args) => T): (...args: Args) => Result<NoVoid<T>, E>;
/**
* Takes a function and returns a version that returns results.
* Takes a function and returns a version that returns results synchronously.
*
* Examples:
* ```ts
* function fn(): string {
* // throws error if failed
* }
* const fn1 = resultify.sync(fn);
* ```
*
* In the context where async functions are not allowed, you can use this function to resultify the sync function.
* If you want to resultify an async function, please use `resultify` instead.
*/
function resultifySync<E>(): CurriedResultifySync<E>;
Expand All @@ -59,8 +77,9 @@ type CurriedResultify<E> = <T, Args extends unknown[]>(
) => (...args: Args) => Promise<Result<NoVoid<Awaited<T>>, E>>;

/**
* Takes a function and returns a version that returns results.
* Takes a function and returns a version that returns results asynchronously.
*
* Examples:
* ```ts
* import fs from 'node:fs/promises';
*
Expand All @@ -73,9 +92,10 @@ function resultify<T, E, Args extends unknown[]>(
fn: (...args: Args) => T | Promise<T>,
): (...args: Args) => Promise<Result<NoVoid<Awaited<T>>, E>>;
/**
* Takes a function and returns a version that returns results.
* Takes a function and returns a version that returns results asynchronously.
* This overloaded function allows you to specify the error type.
*
* Examples:
* ```ts
* import fs from 'node:fs/promises';
*
Expand Down

0 comments on commit 16edf90

Please sign in to comment.