resultar 1.1.3 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-FBVQYZXO.js → chunk-3NDPPYVV.js} +1 -1
- package/dist/chunk-3NDPPYVV.js.map +1 -0
- package/dist/{chunk-UCLMDNNV.js → chunk-UPMJ4PST.js} +118 -67
- package/dist/chunk-UPMJ4PST.js.map +1 -0
- package/dist/error.cjs.map +1 -1
- package/dist/error.d.cts +1 -1
- package/dist/error.d.ts +1 -1
- package/dist/error.js +1 -1
- package/dist/{index-BH-Kjtur.d.cts → index-CKLj0kyj.d.cts} +106 -94
- package/dist/{index-BH-Kjtur.d.ts → index-CKLj0kyj.d.ts} +106 -94
- package/dist/index.cjs +278 -227
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/result-async.cjs +106 -55
- package/dist/result-async.cjs.map +1 -1
- package/dist/result-async.d.cts +1 -1
- package/dist/result-async.d.ts +1 -1
- package/dist/result-async.js +2 -2
- package/dist/result.cjs +112 -61
- package/dist/result.cjs.map +1 -1
- package/dist/result.d.cts +1 -1
- package/dist/result.d.ts +1 -1
- package/dist/result.js +2 -2
- package/dist/utils.cjs +87 -36
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +1 -1
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +2 -2
- package/package.json +28 -92
- package/dist/chunk-FBVQYZXO.js.map +0 -1
- package/dist/chunk-UCLMDNNV.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -35,246 +35,64 @@ __export(src_exports, {
|
|
|
35
35
|
});
|
|
36
36
|
module.exports = __toCommonJS(src_exports);
|
|
37
37
|
|
|
38
|
-
// src/
|
|
39
|
-
var
|
|
40
|
-
|
|
41
|
-
constructor(res) {
|
|
42
|
-
this.innerPromise = res;
|
|
43
|
-
}
|
|
44
|
-
then(successCallback, failureCallback) {
|
|
45
|
-
return this.innerPromise.then(successCallback, failureCallback);
|
|
46
|
-
}
|
|
38
|
+
// src/error.ts
|
|
39
|
+
var defaultErrorConfig = {
|
|
40
|
+
withStackTrace: false
|
|
47
41
|
};
|
|
48
|
-
var
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const newPromise = promise.then((value) => Result.ok(value));
|
|
57
|
-
return new _ResultAsync(newPromise);
|
|
58
|
-
}
|
|
59
|
-
static fromPromise(promise, errorFn) {
|
|
60
|
-
const newPromise = promise.then((value) => Result.ok(value)).catch((error) => Result.err(errorFn(error)));
|
|
61
|
-
return new _ResultAsync(newPromise);
|
|
62
|
-
}
|
|
63
|
-
static combine(asyncResultList) {
|
|
64
|
-
return combineResultAsyncList(asyncResultList);
|
|
65
|
-
}
|
|
66
|
-
static combineWithAllErrors(asyncResultList) {
|
|
67
|
-
return combineResultAsyncListWithAllErrors(
|
|
68
|
-
asyncResultList
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Wraps a async function with a try catch, creating a new function with the same
|
|
73
|
-
* arguments but returning `Ok` if successful, `Err` if the function throws
|
|
74
|
-
*
|
|
75
|
-
* @param fn function to wrap with ok on success or err on failure
|
|
76
|
-
* @param errorFn when an error is thrown, this will wrap the error result if provided
|
|
77
|
-
* @returns a new function that returns a `ResultAsync`
|
|
78
|
-
*/
|
|
79
|
-
static fromThrowable(fn, errorFn) {
|
|
80
|
-
return (...args) => new _ResultAsync(
|
|
81
|
-
(async () => {
|
|
82
|
-
try {
|
|
83
|
-
const v = await fn(...args);
|
|
84
|
-
return Result.ok(v);
|
|
85
|
-
} catch (error) {
|
|
86
|
-
const e = errorFn ? errorFn(error) : error;
|
|
87
|
-
return Result.err(e);
|
|
88
|
-
}
|
|
89
|
-
})()
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
innerPromise;
|
|
93
|
-
constructor(res) {
|
|
94
|
-
this.innerPromise = res;
|
|
95
|
-
}
|
|
96
|
-
then(successCallback, failureCallback) {
|
|
97
|
-
return this.innerPromise.then(successCallback, failureCallback);
|
|
98
|
-
}
|
|
99
|
-
mapErr(f) {
|
|
100
|
-
return new _ResultAsync(this.innerPromise.then(async (res) => {
|
|
101
|
-
if (res.isOk()) {
|
|
102
|
-
return okAsync(res.value);
|
|
103
|
-
}
|
|
104
|
-
return errAsync(await f(res.error));
|
|
105
|
-
}));
|
|
106
|
-
}
|
|
107
|
-
map(f) {
|
|
108
|
-
const x = new _ResultAsync(
|
|
109
|
-
this.innerPromise.then(async (res) => {
|
|
110
|
-
if (res.isErr()) {
|
|
111
|
-
return errAsync(res.error);
|
|
112
|
-
}
|
|
113
|
-
return okAsync(await f(res.value));
|
|
114
|
-
})
|
|
115
|
-
);
|
|
116
|
-
return x;
|
|
117
|
-
}
|
|
118
|
-
andThen(f) {
|
|
119
|
-
return new _ResultAsync(
|
|
120
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
121
|
-
this.innerPromise.then(async (res) => {
|
|
122
|
-
if (res.isErr()) {
|
|
123
|
-
return errAsync(res.error);
|
|
124
|
-
}
|
|
125
|
-
const newValue = f(res.value);
|
|
126
|
-
return newValue instanceof _ResultAsync ? newValue.innerPromise : newValue;
|
|
127
|
-
})
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
if(fCondition) {
|
|
131
|
-
return {
|
|
132
|
-
true: (fTrue) => ({
|
|
133
|
-
false: (fFalse) => new _ResultAsync(this.innerPromise.then(async (res) => {
|
|
134
|
-
if (res.isOk()) {
|
|
135
|
-
const condition = fCondition(res.value);
|
|
136
|
-
return condition ? fTrue(res.value) : fFalse(res.value);
|
|
137
|
-
}
|
|
138
|
-
return errAsync(res.error);
|
|
139
|
-
}))
|
|
140
|
-
})
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
orElse(f) {
|
|
144
|
-
return new _ResultAsync(
|
|
145
|
-
this.innerPromise.then(async (res) => {
|
|
146
|
-
if (res.isErr()) {
|
|
147
|
-
return f(res.error);
|
|
148
|
-
}
|
|
149
|
-
return okAsync(res.value);
|
|
150
|
-
})
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
async match(ok2, err2) {
|
|
154
|
-
return this.innerPromise.then((res) => res.match(ok2, err2));
|
|
155
|
-
}
|
|
156
|
-
async unwrapOr(t) {
|
|
157
|
-
return this.innerPromise.then((res) => res.unwrapOr(t));
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Performs a side effect for the `Ok` variant of `ResultAsync`.
|
|
161
|
-
* This function can be used for control flow based on result values.
|
|
162
|
-
* @param f The function to call if the result is `Ok`
|
|
163
|
-
* @returns `self` if the result is `Err`, otherwise the result of `f`
|
|
164
|
-
*/
|
|
165
|
-
tap(f) {
|
|
166
|
-
return new _ResultAsync(
|
|
167
|
-
this.innerPromise.then(async (res) => {
|
|
168
|
-
if (res.isErr()) {
|
|
169
|
-
return errAsync(res.error);
|
|
170
|
-
}
|
|
171
|
-
try {
|
|
172
|
-
await f(res.value);
|
|
173
|
-
} catch {
|
|
174
|
-
}
|
|
175
|
-
return okAsync(res.value);
|
|
176
|
-
})
|
|
177
|
-
);
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Performs a side effect for the `Err` variant of `ResultAsync`.
|
|
181
|
-
* This function can be used for control flow based on result values.
|
|
182
|
-
* @param f The function to call if the result is `Err`
|
|
183
|
-
* @returns `self` if the result is `Ok`, otherwise the result of `f`
|
|
184
|
-
* @example
|
|
185
|
-
*/
|
|
186
|
-
tapError(f) {
|
|
187
|
-
return new _ResultAsync(
|
|
188
|
-
this.innerPromise.then(async (res) => {
|
|
189
|
-
if (res.isErr()) {
|
|
190
|
-
try {
|
|
191
|
-
await f(res.error);
|
|
192
|
-
} catch {
|
|
193
|
-
}
|
|
194
|
-
return errAsync(res.error);
|
|
195
|
-
}
|
|
196
|
-
return okAsync(res.value);
|
|
197
|
-
})
|
|
198
|
-
);
|
|
199
|
-
}
|
|
200
|
-
finally(f) {
|
|
201
|
-
return new DisposableResultAsync(
|
|
202
|
-
this.innerPromise.then(async (res) => {
|
|
203
|
-
try {
|
|
204
|
-
res.finally(f);
|
|
205
|
-
} catch {
|
|
206
|
-
}
|
|
207
|
-
return res;
|
|
208
|
-
})
|
|
209
|
-
);
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
|
|
213
|
-
*/
|
|
214
|
-
async *safeUnwrap() {
|
|
215
|
-
return yield* await this.innerPromise.then((res) => res.safeUnwrap());
|
|
216
|
-
}
|
|
42
|
+
var createResultarError = (message, result, config = defaultErrorConfig) => {
|
|
43
|
+
const data = result.isOk() ? { type: "Ok", value: result.value } : { type: "Err", value: result.error };
|
|
44
|
+
const maybeStack = config.withStackTrace ? new Error().stack : void 0;
|
|
45
|
+
return {
|
|
46
|
+
data,
|
|
47
|
+
message,
|
|
48
|
+
stack: maybeStack
|
|
49
|
+
};
|
|
217
50
|
};
|
|
218
|
-
var { okAsync, errAsync, fromPromise, fromSafePromise } = ResultAsync;
|
|
219
|
-
var fromThrowableAsync = ResultAsync.fromThrowable;
|
|
220
|
-
function safeTryAsync(body) {
|
|
221
|
-
return new ResultAsync(
|
|
222
|
-
(async () => {
|
|
223
|
-
const n = await body().next();
|
|
224
|
-
if (n.value.isOk()) {
|
|
225
|
-
return okAsync(n.value.value);
|
|
226
|
-
}
|
|
227
|
-
return errAsync(n.value.error);
|
|
228
|
-
})()
|
|
229
|
-
);
|
|
230
|
-
}
|
|
231
51
|
|
|
232
52
|
// src/utils.ts
|
|
233
|
-
var appendValueToEndOfList = (value) => (list) => [...list, value];
|
|
234
53
|
var combineResultAsyncList = (asyncResultList) => ResultAsync.fromSafePromise(Promise.all(asyncResultList)).andThen(
|
|
235
54
|
combineResultList
|
|
236
55
|
);
|
|
237
|
-
var combineResultList = (resultList) =>
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
);
|
|
56
|
+
var combineResultList = (resultList) => {
|
|
57
|
+
let acc = ok([]);
|
|
58
|
+
for (const result of resultList) {
|
|
59
|
+
if (result.isErr()) {
|
|
60
|
+
acc = err(result.error);
|
|
61
|
+
break;
|
|
62
|
+
} else {
|
|
63
|
+
acc.map((list) => list.push(result.value));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return acc;
|
|
67
|
+
};
|
|
242
68
|
var combineResultAsyncListWithAllErrors = (asyncResultList) => {
|
|
243
69
|
const x = ResultAsync.fromSafePromise(Promise.all(asyncResultList)).andThen(
|
|
244
70
|
combineResultListWithAllErrors
|
|
245
71
|
);
|
|
246
72
|
return x;
|
|
247
73
|
};
|
|
248
|
-
var combineResultListWithAllErrors = (resultList) =>
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
);
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
const maybeStack = config.withStackTrace ? new Error().stack : void 0;
|
|
261
|
-
return {
|
|
262
|
-
data,
|
|
263
|
-
message,
|
|
264
|
-
stack: maybeStack
|
|
265
|
-
};
|
|
74
|
+
var combineResultListWithAllErrors = (resultList) => {
|
|
75
|
+
let acc = ok([]);
|
|
76
|
+
for (const result of resultList) {
|
|
77
|
+
if (result.isErr() && acc.isErr()) {
|
|
78
|
+
acc.error.push(result.error);
|
|
79
|
+
} else if (result.isErr() && acc.isOk()) {
|
|
80
|
+
acc = err([result.error]);
|
|
81
|
+
} else if (result.isOk() && acc.isOk()) {
|
|
82
|
+
acc.value.push(result.value);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return acc;
|
|
266
86
|
};
|
|
267
87
|
|
|
268
88
|
// src/result.ts
|
|
269
89
|
var Ok = class {
|
|
270
|
-
// eslint-disable-line @typescript-eslint/class-literal-property-style
|
|
271
90
|
constructor(value) {
|
|
272
91
|
this.value = value;
|
|
273
92
|
}
|
|
274
93
|
ok = true;
|
|
275
94
|
};
|
|
276
95
|
var Err = class {
|
|
277
|
-
// eslint-disable-line @typescript-eslint/class-literal-property-style
|
|
278
96
|
constructor(error) {
|
|
279
97
|
this.error = error;
|
|
280
98
|
}
|
|
@@ -298,6 +116,7 @@ var Result = class _Result {
|
|
|
298
116
|
* @param fn function to wrap with ok on success or err on failure
|
|
299
117
|
* @param errorFn when an error is thrown, this will wrap the error result if provided
|
|
300
118
|
*/
|
|
119
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
301
120
|
static fromThrowable(fn, errorFn) {
|
|
302
121
|
return (...args) => {
|
|
303
122
|
try {
|
|
@@ -437,7 +256,6 @@ var Result = class _Result {
|
|
|
437
256
|
return defaultValue;
|
|
438
257
|
}
|
|
439
258
|
/**
|
|
440
|
-
*
|
|
441
259
|
* Given 2 functions (one for the `Ok` variant and one for the `Err` variant)
|
|
442
260
|
* execute the function that matches the `Result` variant.
|
|
443
261
|
*
|
|
@@ -456,6 +274,19 @@ var Result = class _Result {
|
|
|
456
274
|
}
|
|
457
275
|
return fnErr(this.error);
|
|
458
276
|
}
|
|
277
|
+
/**
|
|
278
|
+
* Performs a side effect no matter what the `Result` variant is.
|
|
279
|
+
*
|
|
280
|
+
* @param fn The function to apply an `OK` value
|
|
281
|
+
* @returns the result of applying `f` or an `Err` untouched
|
|
282
|
+
*/
|
|
283
|
+
log(fn) {
|
|
284
|
+
try {
|
|
285
|
+
fn(this.value, this.error);
|
|
286
|
+
} catch {
|
|
287
|
+
}
|
|
288
|
+
return this;
|
|
289
|
+
}
|
|
459
290
|
/**
|
|
460
291
|
* Performs a side effect for the `Ok` variant of `Result`.
|
|
461
292
|
*
|
|
@@ -468,9 +299,8 @@ var Result = class _Result {
|
|
|
468
299
|
fn(this.value);
|
|
469
300
|
} catch {
|
|
470
301
|
}
|
|
471
|
-
return ok(this.value);
|
|
472
302
|
}
|
|
473
|
-
return
|
|
303
|
+
return this;
|
|
474
304
|
}
|
|
475
305
|
/**
|
|
476
306
|
* Performs a side effect for the `Err` variant of `Result`.
|
|
@@ -479,14 +309,13 @@ var Result = class _Result {
|
|
|
479
309
|
* @returns the result of applying `f` or an `Ok` untouched
|
|
480
310
|
*/
|
|
481
311
|
tapError(fn) {
|
|
482
|
-
if (this.state.ok) {
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
} catch {
|
|
312
|
+
if (!this.state.ok) {
|
|
313
|
+
try {
|
|
314
|
+
fn(this.error);
|
|
315
|
+
} catch {
|
|
316
|
+
}
|
|
488
317
|
}
|
|
489
|
-
return
|
|
318
|
+
return this;
|
|
490
319
|
}
|
|
491
320
|
finally(f) {
|
|
492
321
|
const resultDisposable = new DisposableResult(this, f);
|
|
@@ -575,6 +404,228 @@ function safeTry(body) {
|
|
|
575
404
|
}
|
|
576
405
|
return n.value;
|
|
577
406
|
}
|
|
407
|
+
|
|
408
|
+
// src/result-async.ts
|
|
409
|
+
var DisposableResultAsync = class {
|
|
410
|
+
innerPromise;
|
|
411
|
+
constructor(res) {
|
|
412
|
+
this.innerPromise = res;
|
|
413
|
+
}
|
|
414
|
+
then(successCallback, failureCallback) {
|
|
415
|
+
return this.innerPromise.then(successCallback, failureCallback);
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
var ResultAsync = class _ResultAsync {
|
|
419
|
+
static okAsync(value) {
|
|
420
|
+
return new _ResultAsync(Promise.resolve(Result.ok(value)));
|
|
421
|
+
}
|
|
422
|
+
static errAsync(error) {
|
|
423
|
+
return new _ResultAsync(Promise.resolve(Result.err(error)));
|
|
424
|
+
}
|
|
425
|
+
static fromSafePromise(promise) {
|
|
426
|
+
const newPromise = promise.then((value) => Result.ok(value));
|
|
427
|
+
return new _ResultAsync(newPromise);
|
|
428
|
+
}
|
|
429
|
+
static fromPromise(promise, errorFn) {
|
|
430
|
+
const newPromise = promise.then((value) => Result.ok(value)).catch((error) => Result.err(errorFn(error)));
|
|
431
|
+
return new _ResultAsync(newPromise);
|
|
432
|
+
}
|
|
433
|
+
static combine(asyncResultList) {
|
|
434
|
+
return combineResultAsyncList(asyncResultList);
|
|
435
|
+
}
|
|
436
|
+
static combineWithAllErrors(asyncResultList) {
|
|
437
|
+
return combineResultAsyncListWithAllErrors(
|
|
438
|
+
asyncResultList
|
|
439
|
+
);
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Wraps a async function with a try catch, creating a new function with the same
|
|
443
|
+
* arguments but returning `Ok` if successful, `Err` if the function throws
|
|
444
|
+
*
|
|
445
|
+
* @param fn function to wrap with ok on success or err on failure
|
|
446
|
+
* @param errorFn when an error is thrown, this will wrap the error result if provided
|
|
447
|
+
* @returns a new function that returns a `ResultAsync`
|
|
448
|
+
*/
|
|
449
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
450
|
+
static fromThrowable(fn, errorFn) {
|
|
451
|
+
return (...args) => new _ResultAsync(
|
|
452
|
+
(async () => {
|
|
453
|
+
try {
|
|
454
|
+
const v = await fn(...args);
|
|
455
|
+
return Result.ok(v);
|
|
456
|
+
} catch (error) {
|
|
457
|
+
const e = errorFn ? errorFn(error) : error;
|
|
458
|
+
return Result.err(e);
|
|
459
|
+
}
|
|
460
|
+
})()
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
innerPromise;
|
|
464
|
+
constructor(res) {
|
|
465
|
+
this.innerPromise = res;
|
|
466
|
+
}
|
|
467
|
+
then(successCallback, failureCallback) {
|
|
468
|
+
return this.innerPromise.then(successCallback, failureCallback);
|
|
469
|
+
}
|
|
470
|
+
mapErr(f) {
|
|
471
|
+
return new _ResultAsync(this.innerPromise.then(async (res) => {
|
|
472
|
+
if (res.isOk()) {
|
|
473
|
+
return okAsync(res.value);
|
|
474
|
+
}
|
|
475
|
+
return errAsync(await f(res.error));
|
|
476
|
+
}));
|
|
477
|
+
}
|
|
478
|
+
map(f) {
|
|
479
|
+
const x = new _ResultAsync(
|
|
480
|
+
this.innerPromise.then(async (res) => {
|
|
481
|
+
if (res.isErr()) {
|
|
482
|
+
return errAsync(res.error);
|
|
483
|
+
}
|
|
484
|
+
return okAsync(await f(res.value));
|
|
485
|
+
})
|
|
486
|
+
);
|
|
487
|
+
return x;
|
|
488
|
+
}
|
|
489
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
490
|
+
andThen(f) {
|
|
491
|
+
return new _ResultAsync(
|
|
492
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
493
|
+
this.innerPromise.then(async (res) => {
|
|
494
|
+
if (res.isErr()) {
|
|
495
|
+
return errAsync(res.error);
|
|
496
|
+
}
|
|
497
|
+
const newValue = f(res.value);
|
|
498
|
+
return newValue instanceof _ResultAsync ? newValue.innerPromise : newValue;
|
|
499
|
+
})
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
if(fCondition) {
|
|
503
|
+
return {
|
|
504
|
+
true: (fTrue) => ({
|
|
505
|
+
false: (fFalse) => new _ResultAsync(this.innerPromise.then(async (res) => {
|
|
506
|
+
if (res.isOk()) {
|
|
507
|
+
const condition = fCondition(res.value);
|
|
508
|
+
return condition ? fTrue(res.value) : fFalse(res.value);
|
|
509
|
+
}
|
|
510
|
+
return errAsync(res.error);
|
|
511
|
+
}))
|
|
512
|
+
})
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
516
|
+
orElse(f) {
|
|
517
|
+
return new _ResultAsync(
|
|
518
|
+
this.innerPromise.then(async (res) => {
|
|
519
|
+
if (res.isErr()) {
|
|
520
|
+
return f(res.error);
|
|
521
|
+
}
|
|
522
|
+
return okAsync(res.value);
|
|
523
|
+
})
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
async match(ok2, err2) {
|
|
527
|
+
return this.innerPromise.then((res) => res.match(ok2, err2));
|
|
528
|
+
}
|
|
529
|
+
async unwrapOr(t) {
|
|
530
|
+
return this.innerPromise.then((res) => res.unwrapOr(t));
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Performs a side effect for the `Ok` variant of `ResultAsync`.
|
|
534
|
+
* This function can be used for control flow based on result values.
|
|
535
|
+
* @param f The function to call if the result is `Ok`
|
|
536
|
+
* @returns `self` if the result is `Err`, otherwise the result of `f`
|
|
537
|
+
*/
|
|
538
|
+
log(f) {
|
|
539
|
+
return new _ResultAsync(
|
|
540
|
+
this.innerPromise.then(async (res) => {
|
|
541
|
+
if (res.isOk()) {
|
|
542
|
+
try {
|
|
543
|
+
await f(res.value);
|
|
544
|
+
} catch {
|
|
545
|
+
}
|
|
546
|
+
return okAsync(res.value);
|
|
547
|
+
}
|
|
548
|
+
try {
|
|
549
|
+
await f(void 0, res.error);
|
|
550
|
+
} catch {
|
|
551
|
+
}
|
|
552
|
+
return errAsync(res.error);
|
|
553
|
+
})
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Performs a side effect for the `Ok` variant of `ResultAsync`.
|
|
558
|
+
* This function can be used for control flow based on result values.
|
|
559
|
+
* @param f The function to call if the result is `Ok`
|
|
560
|
+
* @returns `self` if the result is `Err`, otherwise the result of `f`
|
|
561
|
+
*/
|
|
562
|
+
tap(f) {
|
|
563
|
+
return new _ResultAsync(
|
|
564
|
+
this.innerPromise.then(async (res) => {
|
|
565
|
+
if (res.isErr()) {
|
|
566
|
+
return errAsync(res.error);
|
|
567
|
+
}
|
|
568
|
+
try {
|
|
569
|
+
await f(res.value);
|
|
570
|
+
} catch {
|
|
571
|
+
}
|
|
572
|
+
return okAsync(res.value);
|
|
573
|
+
})
|
|
574
|
+
);
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Performs a side effect for the `Err` variant of `ResultAsync`.
|
|
578
|
+
* This function can be used for control flow based on result values.
|
|
579
|
+
* @param f The function to call if the result is `Err`
|
|
580
|
+
* @returns `self` if the result is `Ok`, otherwise the result of `f`
|
|
581
|
+
* @example
|
|
582
|
+
*/
|
|
583
|
+
tapError(f) {
|
|
584
|
+
return new _ResultAsync(
|
|
585
|
+
this.innerPromise.then(async (res) => {
|
|
586
|
+
if (res.isErr()) {
|
|
587
|
+
try {
|
|
588
|
+
await f(res.error);
|
|
589
|
+
} catch {
|
|
590
|
+
}
|
|
591
|
+
return errAsync(res.error);
|
|
592
|
+
}
|
|
593
|
+
return okAsync(res.value);
|
|
594
|
+
})
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
finally(f) {
|
|
598
|
+
return new DisposableResultAsync(
|
|
599
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
600
|
+
this.innerPromise.then(async (res) => {
|
|
601
|
+
try {
|
|
602
|
+
res.finally(f);
|
|
603
|
+
} catch {
|
|
604
|
+
}
|
|
605
|
+
return res;
|
|
606
|
+
})
|
|
607
|
+
);
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
|
|
611
|
+
*/
|
|
612
|
+
async *safeUnwrap() {
|
|
613
|
+
return yield* await this.innerPromise.then((res) => res.safeUnwrap());
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
var { okAsync, errAsync, fromPromise, fromSafePromise } = ResultAsync;
|
|
617
|
+
var fromThrowableAsync = ResultAsync.fromThrowable;
|
|
618
|
+
function safeTryAsync(body) {
|
|
619
|
+
return new ResultAsync(
|
|
620
|
+
(async () => {
|
|
621
|
+
const n = await body().next();
|
|
622
|
+
if (n.value.isOk()) {
|
|
623
|
+
return okAsync(n.value.value);
|
|
624
|
+
}
|
|
625
|
+
return errAsync(n.value.error);
|
|
626
|
+
})()
|
|
627
|
+
);
|
|
628
|
+
}
|
|
578
629
|
// Annotate the CommonJS export names for ESM import in node:
|
|
579
630
|
0 && (module.exports = {
|
|
580
631
|
Result,
|