diff --git a/README.md b/README.md index 4f05bbe..9bbb4f4 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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()(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()(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 diff --git a/src/resultify.ts b/src/resultify.ts index fb4b134..bc25edb 100644 --- a/src/resultify.ts +++ b/src/resultify.ts @@ -24,16 +24,34 @@ type CurriedResultifySync = ( ) => (...args: Args) => Result, 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(fn: (...args: Args) => T): (...args: Args) => Result, 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(): CurriedResultifySync; @@ -59,8 +77,9 @@ type CurriedResultify = ( ) => (...args: Args) => Promise>, 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'; * @@ -73,9 +92,10 @@ function resultify( fn: (...args: Args) => T | Promise, ): (...args: Args) => Promise>, 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'; *