-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathresult-async.ts
316 lines (278 loc) · 10.5 KB
/
result-async.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import type {
Combine,
Dedup,
EmptyArrayToNever,
IsLiteralArray,
MemberListOf,
MembersToUnion,
} from './result'
import { Err, Ok, Result } from './'
import {
combineResultAsyncList,
combineResultAsyncListWithAllErrors,
ExtractErrAsyncTypes,
ExtractOkAsyncTypes,
InferAsyncErrTypes,
InferAsyncOkTypes,
InferErrTypes,
InferOkTypes,
} from './_internals/utils'
export class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
private _promise: Promise<Result<T, E>>
constructor(res: Promise<Result<T, E>>) {
this._promise = res
}
static fromSafePromise<T, E = never>(promise: PromiseLike<T>): ResultAsync<T, E>
static fromSafePromise<T, E = never>(promise: Promise<T>): ResultAsync<T, E> {
const newPromise = promise.then((value: T) => new Ok<T, E>(value))
return new ResultAsync(newPromise)
}
static fromPromise<T, E>(promise: PromiseLike<T>, errorFn: (e: unknown) => E): ResultAsync<T, E>
static fromPromise<T, E>(promise: Promise<T>, errorFn: (e: unknown) => E): ResultAsync<T, E> {
const newPromise = promise
.then((value: T) => new Ok<T, E>(value))
.catch((e) => new Err<T, E>(errorFn(e)))
return new ResultAsync(newPromise)
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static fromThrowable<A extends readonly any[], R, E>(
fn: (...args: A) => Promise<R>,
errorFn?: (err: unknown) => E,
): (...args: A) => ResultAsync<R, E> {
return (...args) => {
return new ResultAsync(
(async () => {
try {
return new Ok(await fn(...args))
} catch (error) {
return new Err(errorFn ? errorFn(error) : error)
}
})(),
)
}
}
static combine<
T extends readonly [ResultAsync<unknown, unknown>, ...ResultAsync<unknown, unknown>[]]
>(asyncResultList: T): CombineResultAsyncs<T>
static combine<T extends readonly ResultAsync<unknown, unknown>[]>(
asyncResultList: T,
): CombineResultAsyncs<T>
static combine<T extends readonly ResultAsync<unknown, unknown>[]>(
asyncResultList: T,
): CombineResultAsyncs<T> {
return (combineResultAsyncList(asyncResultList) as unknown) as CombineResultAsyncs<T>
}
static combineWithAllErrors<
T extends readonly [ResultAsync<unknown, unknown>, ...ResultAsync<unknown, unknown>[]]
>(asyncResultList: T): CombineResultsWithAllErrorsArrayAsync<T>
static combineWithAllErrors<T extends readonly ResultAsync<unknown, unknown>[]>(
asyncResultList: T,
): CombineResultsWithAllErrorsArrayAsync<T>
static combineWithAllErrors<T extends readonly ResultAsync<unknown, unknown>[]>(
asyncResultList: T,
): CombineResultsWithAllErrorsArrayAsync<T> {
return combineResultAsyncListWithAllErrors(
asyncResultList,
) as CombineResultsWithAllErrorsArrayAsync<T>
}
map<A>(f: (t: T) => A | Promise<A>): ResultAsync<A, E> {
return new ResultAsync(
this._promise.then(async (res: Result<T, E>) => {
if (res.isErr()) {
return new Err<A, E>(res.error)
}
return new Ok<A, E>(await f(res.value))
}),
)
}
andThrough<F>(f: (t: T) => Result<unknown, F> | ResultAsync<unknown, F>): ResultAsync<T, E | F> {
return new ResultAsync(
this._promise.then(async (res: Result<T, E>) => {
if (res.isErr()) {
return new Err<T, E>(res.error)
}
const newRes = await f(res.value)
if (newRes.isErr()) {
return new Err<T, F>(newRes.error)
}
return new Ok<T, F>(res.value)
}),
)
}
andTee(f: (t: T) => unknown): ResultAsync<T, E> {
return new ResultAsync(
this._promise.then(async (res: Result<T, E>) => {
if (res.isErr()) {
return new Err<T, E>(res.error)
}
try {
await f(res.value)
} catch (e) {
// Tee does not care about the error
}
return new Ok<T, E>(res.value)
}),
)
}
mapErr<U>(f: (e: E) => U | Promise<U>): ResultAsync<T, U> {
return new ResultAsync(
this._promise.then(async (res: Result<T, E>) => {
if (res.isOk()) {
return new Ok<T, U>(res.value)
}
return new Err<T, U>(await f(res.error))
}),
)
}
andThen<R extends Result<unknown, unknown>>(
f: (t: T) => R,
): ResultAsync<InferOkTypes<R>, InferErrTypes<R> | E>
andThen<R extends ResultAsync<unknown, unknown>>(
f: (t: T) => R,
): ResultAsync<InferAsyncOkTypes<R>, InferAsyncErrTypes<R> | E>
andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F>
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
andThen(f: any): any {
return new ResultAsync(
this._promise.then((res) => {
if (res.isErr()) {
return new Err<never, E>(res.error)
}
const newValue = f(res.value)
return newValue instanceof ResultAsync ? newValue._promise : newValue
}),
)
}
orElse<R extends Result<unknown, unknown>>(
f: (e: E) => R,
): ResultAsync<InferOkTypes<R> | T, InferErrTypes<R>>
orElse<R extends ResultAsync<unknown, unknown>>(
f: (e: E) => R,
): ResultAsync<InferAsyncOkTypes<R> | T, InferAsyncErrTypes<R>>
orElse<U, A>(f: (e: E) => Result<U, A> | ResultAsync<U, A>): ResultAsync<U | T, A>
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
orElse(f: any): any {
return new ResultAsync(
this._promise.then(async (res: Result<T, E>) => {
if (res.isErr()) {
return f(res.error)
}
return new Ok<T, unknown>(res.value)
}),
)
}
match<A, B = A>(ok: (t: T) => A, _err: (e: E) => B): Promise<A | B> {
return this._promise.then((res) => res.match(ok, _err))
}
unwrapOr<A>(t: A): Promise<T | A> {
return this._promise.then((res) => res.unwrapOr(t))
}
/**
* @deprecated will be removed in 9.0.0.
*
* You can use `safeTry` without this method.
* @example
* ```typescript
* safeTry(async function* () {
* const okValue = yield* yourResult
* })
* ```
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
*/
async *safeUnwrap(): AsyncGenerator<Err<never, E>, T> {
return yield* await this._promise.then((res) => res.safeUnwrap())
}
// Makes ResultAsync implement PromiseLike<Result>
then<A, B>(
successCallback?: (res: Result<T, E>) => A | PromiseLike<A>,
failureCallback?: (reason: unknown) => B | PromiseLike<B>,
): PromiseLike<A | B> {
return this._promise.then(successCallback, failureCallback)
}
async *[Symbol.asyncIterator](): AsyncGenerator<Err<never, E>, T> {
const result = await this._promise
if (result.isErr()) {
// @ts-expect-error -- This is structurally equivalent and safe
yield errAsync(result.error)
}
// @ts-expect-error -- This is structurally equivalent and safe
return result.value
}
}
export const okAsync = <T, E = never>(value: T): ResultAsync<T, E> =>
new ResultAsync(Promise.resolve(new Ok<T, E>(value)))
export const errAsync = <T = never, E = unknown>(err: E): ResultAsync<T, E> =>
new ResultAsync(Promise.resolve(new Err<T, E>(err)))
export const fromPromise = ResultAsync.fromPromise
export const fromSafePromise = ResultAsync.fromSafePromise
export const fromAsyncThrowable = ResultAsync.fromThrowable
// Combines the array of async results into one result.
export type CombineResultAsyncs<
T extends readonly ResultAsync<unknown, unknown>[]
> = IsLiteralArray<T> extends 1
? TraverseAsync<UnwrapAsync<T>>
: ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number]>
// Combines the array of async results into one result with all errors.
export type CombineResultsWithAllErrorsArrayAsync<
T extends readonly ResultAsync<unknown, unknown>[]
> = IsLiteralArray<T> extends 1
? TraverseWithAllErrorsAsync<UnwrapAsync<T>>
: ResultAsync<ExtractOkAsyncTypes<T>, ExtractErrAsyncTypes<T>[number][]>
// Unwraps the inner `Result` from a `ResultAsync` for all elements.
type UnwrapAsync<T> = IsLiteralArray<T> extends 1
? Writable<T> extends [infer H, ...infer Rest]
? H extends PromiseLike<infer HI>
? HI extends Result<unknown, unknown>
? [Dedup<HI>, ...UnwrapAsync<Rest>]
: never
: never
: []
: // If we got something too general such as ResultAsync<X, Y>[] then we
// simply need to map it to ResultAsync<X[], Y[]>. Yet `ResultAsync`
// itself is a union therefore it would be enough to cast it to Ok.
T extends Array<infer A>
? A extends PromiseLike<infer HI>
? HI extends Result<infer L, infer R>
? Ok<L, R>[]
: never
: never
: never
// Traverse through the tuples of the async results and create one
// `ResultAsync` where the collected tuples are merged.
type TraverseAsync<T, Depth extends number = 5> = IsLiteralArray<T> extends 1
? Combine<T, Depth> extends [infer Oks, infer Errs]
? ResultAsync<EmptyArrayToNever<Oks>, MembersToUnion<Errs>>
: never
: // The following check is important if we somehow reach to the point of
// checking something similar to ResultAsync<X, Y>[]. In this case we don't
// know the length of the elements, therefore we need to traverse the X and Y
// in a way that the result should contain X[] and Y[].
T extends Array<infer I>
? // The MemberListOf<I> here is to include all possible types. Therefore
// if we face (ResultAsync<X, Y> | ResultAsync<A, B>)[] this type should
// handle the case.
Combine<MemberListOf<I>, Depth> extends [infer Oks, infer Errs]
? // The following `extends unknown[]` checks are just to satisfy the TS.
// we already expect them to be an array.
Oks extends unknown[]
? Errs extends unknown[]
? ResultAsync<EmptyArrayToNever<Oks[number][]>, MembersToUnion<Errs[number][]>>
: ResultAsync<EmptyArrayToNever<Oks[number][]>, Errs>
: // The rest of the conditions are to satisfy the TS and support
// the edge cases which are not really expected to happen.
Errs extends unknown[]
? ResultAsync<Oks, MembersToUnion<Errs[number][]>>
: ResultAsync<Oks, Errs>
: never
: never
// This type is similar to the `TraverseAsync` while the errors are also
// collected in a list. For the checks/conditions made here, see that type
// for the documentation.
type TraverseWithAllErrorsAsync<T, Depth extends number = 5> = TraverseAsync<
T,
Depth
> extends ResultAsync<infer Oks, infer Errs>
? ResultAsync<Oks, Errs[]>
: never
// Converts a reaodnly array into a writable array
type Writable<T> = T extends ReadonlyArray<unknown> ? [...T] : T