Skip to content

Commit a49da24

Browse files
committed
feat(std/results): added fromAsyncThrowable
1 parent 06a4bb1 commit a49da24

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { BlobType } from '../../shared'
2+
3+
import { ResultAsync } from './async'
4+
import { Ok } from './ok'
5+
6+
export const fromAsyncThrowable = <A extends readonly BlobType[], T, R>(
7+
fn: (...args: A) => Promise<T>,
8+
errorFn: (err: unknown) => R
9+
): ((
10+
...args: A
11+
) => ResultAsync<
12+
T,
13+
Std.InferNameType<Std.UnionsToResult<R>>,
14+
Std.InferCauseType<Std.UnionsToResult<R>>
15+
>) => {
16+
return ((...args: BlobType) => {
17+
return new ResultAsync(
18+
(async () => {
19+
try {
20+
return new Ok(await fn(...args))
21+
} catch (error) {
22+
return errorFn(error) as BlobType
23+
}
24+
})()
25+
)
26+
}) as BlobType
27+
}

0 commit comments

Comments
 (0)