resultar 1.1.3 → 1.1.4
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-SGURZSJC.js} +104 -67
- package/dist/chunk-SGURZSJC.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 +254 -217
- 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 +84 -47
- 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 +98 -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 +62 -25
- 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 +23 -77
- package/dist/chunk-FBVQYZXO.js.map +0 -1
- package/dist/chunk-UCLMDNNV.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -35,199 +35,19 @@ __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
53
|
var appendValueToEndOfList = (value) => (list) => [...list, value];
|
|
@@ -251,30 +71,14 @@ var combineResultListWithAllErrors = (resultList) => resultList.reduce(
|
|
|
251
71
|
ok([])
|
|
252
72
|
);
|
|
253
73
|
|
|
254
|
-
// src/error.ts
|
|
255
|
-
var defaultErrorConfig = {
|
|
256
|
-
withStackTrace: false
|
|
257
|
-
};
|
|
258
|
-
var createResultarError = (message, result, config = defaultErrorConfig) => {
|
|
259
|
-
const data = result.isOk() ? { type: "Ok", value: result.value } : { type: "Err", value: result.error };
|
|
260
|
-
const maybeStack = config.withStackTrace ? new Error().stack : void 0;
|
|
261
|
-
return {
|
|
262
|
-
data,
|
|
263
|
-
message,
|
|
264
|
-
stack: maybeStack
|
|
265
|
-
};
|
|
266
|
-
};
|
|
267
|
-
|
|
268
74
|
// src/result.ts
|
|
269
75
|
var Ok = class {
|
|
270
|
-
// eslint-disable-line @typescript-eslint/class-literal-property-style
|
|
271
76
|
constructor(value) {
|
|
272
77
|
this.value = value;
|
|
273
78
|
}
|
|
274
79
|
ok = true;
|
|
275
80
|
};
|
|
276
81
|
var Err = class {
|
|
277
|
-
// eslint-disable-line @typescript-eslint/class-literal-property-style
|
|
278
82
|
constructor(error) {
|
|
279
83
|
this.error = error;
|
|
280
84
|
}
|
|
@@ -298,6 +102,7 @@ var Result = class _Result {
|
|
|
298
102
|
* @param fn function to wrap with ok on success or err on failure
|
|
299
103
|
* @param errorFn when an error is thrown, this will wrap the error result if provided
|
|
300
104
|
*/
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
301
106
|
static fromThrowable(fn, errorFn) {
|
|
302
107
|
return (...args) => {
|
|
303
108
|
try {
|
|
@@ -437,7 +242,6 @@ var Result = class _Result {
|
|
|
437
242
|
return defaultValue;
|
|
438
243
|
}
|
|
439
244
|
/**
|
|
440
|
-
*
|
|
441
245
|
* Given 2 functions (one for the `Ok` variant and one for the `Err` variant)
|
|
442
246
|
* execute the function that matches the `Result` variant.
|
|
443
247
|
*
|
|
@@ -456,6 +260,19 @@ var Result = class _Result {
|
|
|
456
260
|
}
|
|
457
261
|
return fnErr(this.error);
|
|
458
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* Performs a side effect no matter what the `Result` variant is.
|
|
265
|
+
*
|
|
266
|
+
* @param fn The function to apply an `OK` value
|
|
267
|
+
* @returns the result of applying `f` or an `Err` untouched
|
|
268
|
+
*/
|
|
269
|
+
log(fn) {
|
|
270
|
+
try {
|
|
271
|
+
fn(this.value, this.error);
|
|
272
|
+
} catch {
|
|
273
|
+
}
|
|
274
|
+
return this;
|
|
275
|
+
}
|
|
459
276
|
/**
|
|
460
277
|
* Performs a side effect for the `Ok` variant of `Result`.
|
|
461
278
|
*
|
|
@@ -468,9 +285,8 @@ var Result = class _Result {
|
|
|
468
285
|
fn(this.value);
|
|
469
286
|
} catch {
|
|
470
287
|
}
|
|
471
|
-
return ok(this.value);
|
|
472
288
|
}
|
|
473
|
-
return
|
|
289
|
+
return this;
|
|
474
290
|
}
|
|
475
291
|
/**
|
|
476
292
|
* Performs a side effect for the `Err` variant of `Result`.
|
|
@@ -479,14 +295,13 @@ var Result = class _Result {
|
|
|
479
295
|
* @returns the result of applying `f` or an `Ok` untouched
|
|
480
296
|
*/
|
|
481
297
|
tapError(fn) {
|
|
482
|
-
if (this.state.ok) {
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
} catch {
|
|
298
|
+
if (!this.state.ok) {
|
|
299
|
+
try {
|
|
300
|
+
fn(this.error);
|
|
301
|
+
} catch {
|
|
302
|
+
}
|
|
488
303
|
}
|
|
489
|
-
return
|
|
304
|
+
return this;
|
|
490
305
|
}
|
|
491
306
|
finally(f) {
|
|
492
307
|
const resultDisposable = new DisposableResult(this, f);
|
|
@@ -575,6 +390,228 @@ function safeTry(body) {
|
|
|
575
390
|
}
|
|
576
391
|
return n.value;
|
|
577
392
|
}
|
|
393
|
+
|
|
394
|
+
// src/result-async.ts
|
|
395
|
+
var DisposableResultAsync = class {
|
|
396
|
+
innerPromise;
|
|
397
|
+
constructor(res) {
|
|
398
|
+
this.innerPromise = res;
|
|
399
|
+
}
|
|
400
|
+
then(successCallback, failureCallback) {
|
|
401
|
+
return this.innerPromise.then(successCallback, failureCallback);
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
var ResultAsync = class _ResultAsync {
|
|
405
|
+
static okAsync(value) {
|
|
406
|
+
return new _ResultAsync(Promise.resolve(Result.ok(value)));
|
|
407
|
+
}
|
|
408
|
+
static errAsync(error) {
|
|
409
|
+
return new _ResultAsync(Promise.resolve(Result.err(error)));
|
|
410
|
+
}
|
|
411
|
+
static fromSafePromise(promise) {
|
|
412
|
+
const newPromise = promise.then((value) => Result.ok(value));
|
|
413
|
+
return new _ResultAsync(newPromise);
|
|
414
|
+
}
|
|
415
|
+
static fromPromise(promise, errorFn) {
|
|
416
|
+
const newPromise = promise.then((value) => Result.ok(value)).catch((error) => Result.err(errorFn(error)));
|
|
417
|
+
return new _ResultAsync(newPromise);
|
|
418
|
+
}
|
|
419
|
+
static combine(asyncResultList) {
|
|
420
|
+
return combineResultAsyncList(asyncResultList);
|
|
421
|
+
}
|
|
422
|
+
static combineWithAllErrors(asyncResultList) {
|
|
423
|
+
return combineResultAsyncListWithAllErrors(
|
|
424
|
+
asyncResultList
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Wraps a async function with a try catch, creating a new function with the same
|
|
429
|
+
* arguments but returning `Ok` if successful, `Err` if the function throws
|
|
430
|
+
*
|
|
431
|
+
* @param fn function to wrap with ok on success or err on failure
|
|
432
|
+
* @param errorFn when an error is thrown, this will wrap the error result if provided
|
|
433
|
+
* @returns a new function that returns a `ResultAsync`
|
|
434
|
+
*/
|
|
435
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
436
|
+
static fromThrowable(fn, errorFn) {
|
|
437
|
+
return (...args) => new _ResultAsync(
|
|
438
|
+
(async () => {
|
|
439
|
+
try {
|
|
440
|
+
const v = await fn(...args);
|
|
441
|
+
return Result.ok(v);
|
|
442
|
+
} catch (error) {
|
|
443
|
+
const e = errorFn ? errorFn(error) : error;
|
|
444
|
+
return Result.err(e);
|
|
445
|
+
}
|
|
446
|
+
})()
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
innerPromise;
|
|
450
|
+
constructor(res) {
|
|
451
|
+
this.innerPromise = res;
|
|
452
|
+
}
|
|
453
|
+
then(successCallback, failureCallback) {
|
|
454
|
+
return this.innerPromise.then(successCallback, failureCallback);
|
|
455
|
+
}
|
|
456
|
+
mapErr(f) {
|
|
457
|
+
return new _ResultAsync(this.innerPromise.then(async (res) => {
|
|
458
|
+
if (res.isOk()) {
|
|
459
|
+
return okAsync(res.value);
|
|
460
|
+
}
|
|
461
|
+
return errAsync(await f(res.error));
|
|
462
|
+
}));
|
|
463
|
+
}
|
|
464
|
+
map(f) {
|
|
465
|
+
const x = new _ResultAsync(
|
|
466
|
+
this.innerPromise.then(async (res) => {
|
|
467
|
+
if (res.isErr()) {
|
|
468
|
+
return errAsync(res.error);
|
|
469
|
+
}
|
|
470
|
+
return okAsync(await f(res.value));
|
|
471
|
+
})
|
|
472
|
+
);
|
|
473
|
+
return x;
|
|
474
|
+
}
|
|
475
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
476
|
+
andThen(f) {
|
|
477
|
+
return new _ResultAsync(
|
|
478
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
479
|
+
this.innerPromise.then(async (res) => {
|
|
480
|
+
if (res.isErr()) {
|
|
481
|
+
return errAsync(res.error);
|
|
482
|
+
}
|
|
483
|
+
const newValue = f(res.value);
|
|
484
|
+
return newValue instanceof _ResultAsync ? newValue.innerPromise : newValue;
|
|
485
|
+
})
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
if(fCondition) {
|
|
489
|
+
return {
|
|
490
|
+
true: (fTrue) => ({
|
|
491
|
+
false: (fFalse) => new _ResultAsync(this.innerPromise.then(async (res) => {
|
|
492
|
+
if (res.isOk()) {
|
|
493
|
+
const condition = fCondition(res.value);
|
|
494
|
+
return condition ? fTrue(res.value) : fFalse(res.value);
|
|
495
|
+
}
|
|
496
|
+
return errAsync(res.error);
|
|
497
|
+
}))
|
|
498
|
+
})
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
502
|
+
orElse(f) {
|
|
503
|
+
return new _ResultAsync(
|
|
504
|
+
this.innerPromise.then(async (res) => {
|
|
505
|
+
if (res.isErr()) {
|
|
506
|
+
return f(res.error);
|
|
507
|
+
}
|
|
508
|
+
return okAsync(res.value);
|
|
509
|
+
})
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
async match(ok2, err2) {
|
|
513
|
+
return this.innerPromise.then((res) => res.match(ok2, err2));
|
|
514
|
+
}
|
|
515
|
+
async unwrapOr(t) {
|
|
516
|
+
return this.innerPromise.then((res) => res.unwrapOr(t));
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Performs a side effect for the `Ok` variant of `ResultAsync`.
|
|
520
|
+
* This function can be used for control flow based on result values.
|
|
521
|
+
* @param f The function to call if the result is `Ok`
|
|
522
|
+
* @returns `self` if the result is `Err`, otherwise the result of `f`
|
|
523
|
+
*/
|
|
524
|
+
log(f) {
|
|
525
|
+
return new _ResultAsync(
|
|
526
|
+
this.innerPromise.then(async (res) => {
|
|
527
|
+
if (res.isOk()) {
|
|
528
|
+
try {
|
|
529
|
+
await f(res.value);
|
|
530
|
+
} catch {
|
|
531
|
+
}
|
|
532
|
+
return okAsync(res.value);
|
|
533
|
+
}
|
|
534
|
+
try {
|
|
535
|
+
await f(void 0, res.error);
|
|
536
|
+
} catch {
|
|
537
|
+
}
|
|
538
|
+
return errAsync(res.error);
|
|
539
|
+
})
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Performs a side effect for the `Ok` variant of `ResultAsync`.
|
|
544
|
+
* This function can be used for control flow based on result values.
|
|
545
|
+
* @param f The function to call if the result is `Ok`
|
|
546
|
+
* @returns `self` if the result is `Err`, otherwise the result of `f`
|
|
547
|
+
*/
|
|
548
|
+
tap(f) {
|
|
549
|
+
return new _ResultAsync(
|
|
550
|
+
this.innerPromise.then(async (res) => {
|
|
551
|
+
if (res.isErr()) {
|
|
552
|
+
return errAsync(res.error);
|
|
553
|
+
}
|
|
554
|
+
try {
|
|
555
|
+
await f(res.value);
|
|
556
|
+
} catch {
|
|
557
|
+
}
|
|
558
|
+
return okAsync(res.value);
|
|
559
|
+
})
|
|
560
|
+
);
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Performs a side effect for the `Err` variant of `ResultAsync`.
|
|
564
|
+
* This function can be used for control flow based on result values.
|
|
565
|
+
* @param f The function to call if the result is `Err`
|
|
566
|
+
* @returns `self` if the result is `Ok`, otherwise the result of `f`
|
|
567
|
+
* @example
|
|
568
|
+
*/
|
|
569
|
+
tapError(f) {
|
|
570
|
+
return new _ResultAsync(
|
|
571
|
+
this.innerPromise.then(async (res) => {
|
|
572
|
+
if (res.isErr()) {
|
|
573
|
+
try {
|
|
574
|
+
await f(res.error);
|
|
575
|
+
} catch {
|
|
576
|
+
}
|
|
577
|
+
return errAsync(res.error);
|
|
578
|
+
}
|
|
579
|
+
return okAsync(res.value);
|
|
580
|
+
})
|
|
581
|
+
);
|
|
582
|
+
}
|
|
583
|
+
finally(f) {
|
|
584
|
+
return new DisposableResultAsync(
|
|
585
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
586
|
+
this.innerPromise.then(async (res) => {
|
|
587
|
+
try {
|
|
588
|
+
res.finally(f);
|
|
589
|
+
} catch {
|
|
590
|
+
}
|
|
591
|
+
return res;
|
|
592
|
+
})
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Emulates Rust's `?` operator in `safeTry`'s body. See also `safeTry`.
|
|
597
|
+
*/
|
|
598
|
+
async *safeUnwrap() {
|
|
599
|
+
return yield* await this.innerPromise.then((res) => res.safeUnwrap());
|
|
600
|
+
}
|
|
601
|
+
};
|
|
602
|
+
var { okAsync, errAsync, fromPromise, fromSafePromise } = ResultAsync;
|
|
603
|
+
var fromThrowableAsync = ResultAsync.fromThrowable;
|
|
604
|
+
function safeTryAsync(body) {
|
|
605
|
+
return new ResultAsync(
|
|
606
|
+
(async () => {
|
|
607
|
+
const n = await body().next();
|
|
608
|
+
if (n.value.isOk()) {
|
|
609
|
+
return okAsync(n.value.value);
|
|
610
|
+
}
|
|
611
|
+
return errAsync(n.value.error);
|
|
612
|
+
})()
|
|
613
|
+
);
|
|
614
|
+
}
|
|
578
615
|
// Annotate the CommonJS export names for ESM import in node:
|
|
579
616
|
0 && (module.exports = {
|
|
580
617
|
Result,
|