-
Notifications
You must be signed in to change notification settings - Fork 0
/
errThen.ts
42 lines (37 loc) · 1.4 KB
/
errThen.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import type { MaybePromise } from '@/types'
import { isPromise } from '@/utils'
import type { Result } from './result'
import { err, ok } from './result'
/**
* Call `fn` if `result` is failure.
*
* @example
* ```typescript
* const userInfo = (await errThen(queryUserInfo, err(userId))).unwrap() // throw an error
* // with flow
* const userInfo = (await flow(ok(userId), andThen(queryUserInfo), errThen(notify))).unwrapOr(defaultUserInfo)
* ```
*
* {@link https://github.com/niuiic/fx-flow/blob/main/packages/test/src/flow/errThen.spec.ts | More examples}
*/
function errThen<A>(fn: (err: string) => Promise<unknown>, result: Result<A>): MaybePromise<Result<A>>
function errThen<A>(fn: (err: string) => Promise<unknown>): (result: Result<A>) => MaybePromise<Result<A>>
function errThen<A>(fn: (err: string) => unknown, result: Result<A>): Result<A>
function errThen<A>(fn: (err: string) => unknown): (result: Result<A>) => Result<A>
function errThen<A>(
fn: (err: string) => MaybePromise<unknown>,
result?: Result<A>
): MaybePromise<Result<A>> | ((result: Result<A>) => MaybePromise<Result<A>>) {
if (result === undefined) {
return (result) => errThen(fn, result)
}
if (result.isErr()) {
const res = fn(result.error()!)
if (isPromise(res)) {
return res.then<Result<A>>(() => err(result.error()!))
}
return err(result.error()!)
}
return ok(result.unwrap())
}
export { errThen }