@qlever-llc/result 0.6.0

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.
@@ -0,0 +1,1038 @@
1
+ "use strict";
2
+ /**
3
+ * A class-based Result<T, E> system inspired by Rust's Result type.
4
+ *
5
+ * This module provides Result and AsyncResult classes for elegant error handling
6
+ * with method chaining and the `take()` pattern for early returns.
7
+ *
8
+ * @module @qlever-llc/result
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.AsyncResult = exports.Result = void 0;
12
+ const error_js_1 = require("./error.js");
13
+ function isOkValue(value) {
14
+ return value.success;
15
+ }
16
+ function isErrValue(value) {
17
+ return !value.success;
18
+ }
19
+ /**
20
+ * A synchronous Result class that represents either success (Ok) or failure (Err).
21
+ *
22
+ * Provides method chaining for transformations and the `take()` pattern for
23
+ * unwrapping values with early returns.
24
+ *
25
+ * @template T - The type of the success value
26
+ * @template E - The type of the error (must extend BaseError)
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * function divide(a: number, b: number): Result<number, ValidationError> {
31
+ * if (b === 0) {
32
+ * return Result.err(new ValidationError("Division by zero"));
33
+ * }
34
+ * return Result.ok(a / b);
35
+ * }
36
+ *
37
+ * const result = divide(10, 2)
38
+ * .map(x => x * 2)
39
+ * .map(x => x + 1);
40
+ *
41
+ * const value = result.take();
42
+ * if (isErr(value)) return value;
43
+ * console.log(value); // 11
44
+ * ```
45
+ */
46
+ class Result {
47
+ constructor(_value) {
48
+ Object.defineProperty(this, "_value", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: _value
53
+ });
54
+ }
55
+ /**
56
+ * Creates a successful Result containing a value.
57
+ *
58
+ * @template T - The type of the success value
59
+ * @template E - The type of the error (defaults to never)
60
+ * @param value - The success value to wrap
61
+ * @returns A Result instance containing the value
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * const result = Result.ok(42);
66
+ * const value = result.take();
67
+ * if (!isErr(value)) {
68
+ * console.log(value); // 42
69
+ * }
70
+ * ```
71
+ */
72
+ static ok(value) {
73
+ return new Result({ success: true, value });
74
+ }
75
+ /**
76
+ * Creates a failed Result containing an error.
77
+ *
78
+ * @template E - The type of the error (must extend BaseError)
79
+ * @template T - The type of the success value (defaults to never)
80
+ * @param error - The error to wrap
81
+ * @returns A Result instance containing the error
82
+ *
83
+ * @example
84
+ * ```typescript
85
+ * const result = Result.err(new ValidationError("Invalid input"));
86
+ * const value = result.take();
87
+ * if (isErr(value)) {
88
+ * console.error(value.error.message); // "Invalid input"
89
+ * }
90
+ * ```
91
+ */
92
+ static err(error) {
93
+ return new Result({ success: false, error });
94
+ }
95
+ /**
96
+ * Wraps a function that might throw into a Result.
97
+ *
98
+ * Catches any exceptions and wraps them in UnexpectedError.
99
+ *
100
+ * @template T - The type of the return value
101
+ * @param fn - Function that might throw
102
+ * @param context - Optional context to add to the error
103
+ * @returns Ok with the return value, or Err with UnexpectedError
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * const obj = Result.try(() =>
108
+ * typeof data === "string" ? JSON.parse(data) : data
109
+ * );
110
+ *
111
+ * const value = obj.take();
112
+ * if (isErr(value)) {
113
+ * console.error("Parse failed:", value.error);
114
+ * return;
115
+ * }
116
+ * console.log(value); // parsed object
117
+ * ```
118
+ */
119
+ static try(fn, context) {
120
+ try {
121
+ return Result.ok(fn());
122
+ }
123
+ catch (cause) {
124
+ return Result.err(new error_js_1.UnexpectedError({ cause }).withContext(context));
125
+ }
126
+ }
127
+ /**
128
+ * Type guard to check if a value is an Ok Result.
129
+ *
130
+ * @template T - The type of the success value
131
+ * @template E - The type of the error
132
+ * @param result - The Result to check
133
+ * @returns True if the result is Ok, false otherwise
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * const result = Result.ok(42);
138
+ * if (Result.isOk(result)) {
139
+ * // TypeScript knows result is Ok<number> here
140
+ * }
141
+ * ```
142
+ */
143
+ static isOk(result) {
144
+ return result.isOk();
145
+ }
146
+ static isErr(value) {
147
+ // Check if it's a Result instance
148
+ if (value instanceof Result) {
149
+ return value.isErr();
150
+ }
151
+ // For non-Result values, return false
152
+ return false;
153
+ }
154
+ /**
155
+ * Combines multiple Results into a single Result containing an array.
156
+ *
157
+ * If all Results are Ok, returns Ok with an array of all values.
158
+ * If any Result is Err, returns the first Err encountered.
159
+ *
160
+ * @template T - The type of the Ok values
161
+ * @template E - The type of the errors
162
+ * @param results - Array of Results to combine
163
+ * @returns Ok with array of values, or the first Err
164
+ *
165
+ * @example
166
+ * ```typescript
167
+ * const results = [Result.ok(1), Result.ok(2), Result.ok(3)];
168
+ * const combined = Result.all(results);
169
+ * // Ok([1, 2, 3])
170
+ *
171
+ * const withError = [Result.ok(1), Result.err(new ValidationError("Failed")), Result.ok(3)];
172
+ * const combined2 = Result.all(withError);
173
+ * // Err(ValidationError)
174
+ * ```
175
+ */
176
+ static all(results) {
177
+ const values = [];
178
+ for (const result of results) {
179
+ if (result.isErr()) {
180
+ return result;
181
+ }
182
+ const resultValue = result._unsafeValue();
183
+ if (resultValue.success) {
184
+ values.push(resultValue.value);
185
+ }
186
+ }
187
+ return Result.ok(values);
188
+ }
189
+ /**
190
+ * Returns the first Ok result from an array of Results.
191
+ *
192
+ * If any Result is Ok, returns that Ok result.
193
+ * If all Results are Err, returns the last Err.
194
+ *
195
+ * @template T - The type of the Ok values
196
+ * @template E - The type of the errors
197
+ * @param results - Array of Results to check
198
+ * @returns The first Ok, or the last Err if all failed
199
+ *
200
+ * @example
201
+ * ```typescript
202
+ * const results = [Result.err(new Error("e1")), Result.ok(2), Result.ok(3)];
203
+ * const first = Result.any(results);
204
+ * // Ok(2)
205
+ *
206
+ * const allErrors = [
207
+ * Result.err(new Error("e1")),
208
+ * Result.err(new Error("e2")),
209
+ * Result.err(new Error("e3"))
210
+ * ];
211
+ * const first2 = Result.any(allErrors);
212
+ * // Err(Error("e3")) - the last error
213
+ * ```
214
+ */
215
+ static any(results) {
216
+ for (const result of results) {
217
+ if (result.isOk()) {
218
+ return result;
219
+ }
220
+ }
221
+ return results[results.length - 1];
222
+ }
223
+ /**
224
+ * Type guard to check if this Result is Ok.
225
+ *
226
+ * @returns True if this result is Ok, false otherwise
227
+ *
228
+ * @example
229
+ * ```typescript
230
+ * const result = Result.ok(42);
231
+ * if (result.isOk()) {
232
+ * // TypeScript knows result is Ok here
233
+ * }
234
+ * ```
235
+ */
236
+ isOk() {
237
+ return this._value.success === true;
238
+ }
239
+ /**
240
+ * Type guard to check if this Result is Err.
241
+ *
242
+ * @returns True if this result is Err, false otherwise
243
+ *
244
+ * @example
245
+ * ```typescript
246
+ * const result = Result.err(new ValidationError("Failed"));
247
+ * if (result.isErr()) {
248
+ * // TypeScript knows result is Err here
249
+ * }
250
+ * ```
251
+ */
252
+ isErr() {
253
+ return this._value.success === false;
254
+ }
255
+ /**
256
+ * Transforms the Ok value using a mapper function, leaving Err untouched.
257
+ *
258
+ * @template U - The type of the transformed value
259
+ * @param fn - Function to transform the Ok value
260
+ * @returns A new Result with the transformed value, or the original Err
261
+ *
262
+ * @example
263
+ * ```typescript
264
+ * const result = Result.ok(5)
265
+ * .map(x => x * 2)
266
+ * .map(x => x.toString());
267
+ *
268
+ * const value = result.take();
269
+ * if (!isErr(value)) {
270
+ * console.log(value); // "10"
271
+ * }
272
+ * ```
273
+ */
274
+ map(fn) {
275
+ const value = this._value;
276
+ if (isOkValue(value)) {
277
+ return Result.ok(fn(value.value));
278
+ }
279
+ return Result.err(value.error);
280
+ }
281
+ /**
282
+ * Transforms the Err value using a mapper function, leaving Ok untouched.
283
+ *
284
+ * @template F - The type of the transformed error
285
+ * @param fn - Function to transform the Err value
286
+ * @returns A new Result with the transformed error, or the original Ok
287
+ *
288
+ * @example
289
+ * ```typescript
290
+ * const result = Result.err(new ValidationError("Failed"))
291
+ * .mapErr(e => new NetworkError({ cause: e }));
292
+ * ```
293
+ */
294
+ mapErr(fn) {
295
+ const value = this._value;
296
+ if (isOkValue(value)) {
297
+ return Result.ok(value.value);
298
+ }
299
+ return Result.err(fn(value.error));
300
+ }
301
+ /**
302
+ * Chains operations that return Results (also known as flatMap).
303
+ *
304
+ * If this Result is Ok, calls the function with the Ok value and returns its Result.
305
+ * If this Result is Err, returns the Err without calling the function.
306
+ *
307
+ * @template U - The type of the new Ok value
308
+ * @template F - The type of the new error
309
+ * @param fn - Function that takes the Ok value and returns a new Result
310
+ * @returns The Result from calling fn, or the original Err
311
+ *
312
+ * @example
313
+ * ```typescript
314
+ * function parseNumber(s: string): Result<number, ValidationError> {
315
+ * const n = Number(s);
316
+ * if (isNaN(n)) {
317
+ * return Result.err(new ValidationError("Not a number"));
318
+ * }
319
+ * return Result.ok(n);
320
+ * }
321
+ *
322
+ * const result = Result.ok("42")
323
+ * .andThen(parseNumber)
324
+ * .map(x => x * 2);
325
+ * ```
326
+ */
327
+ andThen(fn) {
328
+ const value = this._value;
329
+ if (isOkValue(value)) {
330
+ return fn(value.value);
331
+ }
332
+ return Result.err(value.error);
333
+ }
334
+ /**
335
+ * Extracts the value from Ok or returns the Err for early returns.
336
+ *
337
+ * This is the equivalent of Rust's `?` operator. Use it with `isErr()` for
338
+ * early returns in functions that return Results.
339
+ *
340
+ * Returns either:
341
+ * - The unwrapped value T if this result is Ok
342
+ * - An Err Result<never, E> if this result is Err (can be directly returned)
343
+ *
344
+ * @returns The unwrapped value or Err Result
345
+ *
346
+ * @example
347
+ * ```typescript
348
+ * function processData(input: string): Result<number, ValidationError> {
349
+ * const parsed = parseInput(input).take();
350
+ * if (isErr(parsed)) return parsed;
351
+ *
352
+ * const validated = validate(parsed).take();
353
+ * if (isErr(validated)) return validated;
354
+ *
355
+ * return Result.ok(validated * 2);
356
+ * }
357
+ * ```
358
+ */
359
+ take() {
360
+ const value = this._value;
361
+ if (isOkValue(value)) {
362
+ return value.value;
363
+ }
364
+ return Result.err(value.error);
365
+ }
366
+ /**
367
+ * Adds context to an Err result for early returns.
368
+ * Chainable with take() for adding context when propagating errors.
369
+ *
370
+ * @param message - Context message describing the operation that failed
371
+ * @param extra - Optional additional context data
372
+ * @returns This Result with context added to the error
373
+ *
374
+ * @example
375
+ * ```typescript
376
+ * const user = await getUser(id).take();
377
+ * if (isErr(user)) return user.context("failed to fetch user");
378
+ *
379
+ * // With extra data:
380
+ * if (isErr(user)) return user.context("failed to fetch user", { userId: id });
381
+ * ```
382
+ */
383
+ context(message, extra) {
384
+ const value = this._value;
385
+ if (isErrValue(value)) {
386
+ const contextData = extra ? { message, ...extra } : { message };
387
+ value.error.withContext(contextData);
388
+ }
389
+ return this;
390
+ }
391
+ /**
392
+ * Pattern matching for Results - handle both Ok and Err cases.
393
+ *
394
+ * @template U - The type of the return value
395
+ * @param pattern - Object with ok and err handler functions
396
+ * @returns The result of calling either the ok or err handler
397
+ *
398
+ * @example
399
+ * ```typescript
400
+ * const message = result.match({
401
+ * ok: (value) => `Success: ${value}`,
402
+ * err: (error) => `Error: ${error.message}`
403
+ * });
404
+ * ```
405
+ */
406
+ match(pattern) {
407
+ const value = this._value;
408
+ if (isOkValue(value)) {
409
+ return pattern.ok(value.value);
410
+ }
411
+ return pattern.err(value.error);
412
+ }
413
+ /**
414
+ * Returns the Ok value or a default value if Err.
415
+ *
416
+ * @template U - The type of the default value
417
+ * @param defaultValue - The value to return if this result is Err
418
+ * @returns The Ok value or the default value
419
+ *
420
+ * @example
421
+ * ```typescript
422
+ * const value = result.unwrapOr(0);
423
+ * console.log(value); // 42 or 0
424
+ * ```
425
+ */
426
+ unwrapOr(defaultValue) {
427
+ const value = this._value;
428
+ if (isOkValue(value)) {
429
+ return value.value;
430
+ }
431
+ return defaultValue;
432
+ }
433
+ /**
434
+ * Returns the Ok value or computes a default from the error.
435
+ *
436
+ * @template U - The type of the default value
437
+ * @param fn - Function to compute the default value from the error
438
+ * @returns The Ok value or the computed default value
439
+ *
440
+ * @example
441
+ * ```typescript
442
+ * const value = result.unwrapOrElse(error => {
443
+ * console.error(error);
444
+ * return 0;
445
+ * });
446
+ * ```
447
+ */
448
+ unwrapOrElse(fn) {
449
+ const value = this._value;
450
+ if (isOkValue(value)) {
451
+ return value.value;
452
+ }
453
+ return fn(value.error);
454
+ }
455
+ /**
456
+ * Returns this result if Ok, otherwise returns the fallback result.
457
+ *
458
+ * @template U - The type of the fallback Ok value
459
+ * @param other - The fallback Result to use if this is Err
460
+ * @returns This result if Ok, otherwise the fallback
461
+ *
462
+ * @example
463
+ * ```typescript
464
+ * const result = fetchFromCache()
465
+ * .or(fetchFromDatabase())
466
+ * .or(fetchFromAPI());
467
+ * ```
468
+ */
469
+ or(other) {
470
+ if (isOkValue(this._value)) {
471
+ return this;
472
+ }
473
+ return other;
474
+ }
475
+ /**
476
+ * Returns this result if Ok, otherwise computes a fallback from the error.
477
+ *
478
+ * @template R - The Result type returned by the fallback function
479
+ * @param fn - Function to compute a fallback Result from the error
480
+ * @returns This result if Ok, otherwise the computed fallback
481
+ *
482
+ * @example
483
+ * ```typescript
484
+ * const result = fetchData().orElse(error => {
485
+ * console.warn("Primary failed, trying backup");
486
+ * return fetchBackup();
487
+ * });
488
+ * ```
489
+ */
490
+ orElse(fn) {
491
+ const value = this._value;
492
+ if (isOkValue(value)) {
493
+ return Result.ok(value.value);
494
+ }
495
+ return fn(value.error);
496
+ }
497
+ /**
498
+ * Performs a side effect on the Ok value without changing the Result.
499
+ *
500
+ * @param fn - Function to call with the Ok value (if Ok)
501
+ * @returns This Result, unchanged
502
+ *
503
+ * @example
504
+ * ```typescript
505
+ * const result = fetchUser("123")
506
+ * .inspect(user => console.log("Fetched:", user))
507
+ * .map(user => user.name);
508
+ * ```
509
+ */
510
+ inspect(fn) {
511
+ const value = this._value;
512
+ if (isOkValue(value)) {
513
+ fn(value.value);
514
+ }
515
+ return this;
516
+ }
517
+ /**
518
+ * Performs a side effect on the Err value without changing the Result.
519
+ *
520
+ * @param fn - Function to call with the error value (if Err)
521
+ * @returns This Result, unchanged
522
+ *
523
+ * @example
524
+ * ```typescript
525
+ * const result = fetchUser("123")
526
+ * .inspectErr(error => console.error("Failed:", error))
527
+ * .map(user => user.name);
528
+ * ```
529
+ */
530
+ inspectErr(fn) {
531
+ const value = this._value;
532
+ if (isErrValue(value)) {
533
+ fn(value.error);
534
+ }
535
+ return this;
536
+ }
537
+ /**
538
+ * Gets the error from an Err Result.
539
+ *
540
+ * Only call this after checking `isErr()`. If called on Ok, throws an error.
541
+ *
542
+ * @returns The error value
543
+ *
544
+ * @example
545
+ * ```typescript
546
+ * const result = err(new ValidationError("Failed"));
547
+ * if (result.isErr()) {
548
+ * console.log(result.error.message); // "Failed"
549
+ * }
550
+ * ```
551
+ */
552
+ get error() {
553
+ const value = this._value;
554
+ if (isErrValue(value)) {
555
+ return value.error;
556
+ }
557
+ throw new Error("Called .error on an Ok Result");
558
+ }
559
+ /**
560
+ * Internal method to get the raw value (for testing/debugging).
561
+ * Not recommended for general use - prefer take() instead.
562
+ */
563
+ _unsafeValue() {
564
+ return this._value;
565
+ }
566
+ }
567
+ exports.Result = Result;
568
+ /**
569
+ * An asynchronous Result class that represents a Promise of Result<T, E>.
570
+ *
571
+ * Implements PromiseLike to be awaitable, and provides async versions of
572
+ * all Result methods that return AsyncResult for seamless chaining.
573
+ *
574
+ * @template T - The type of the success value
575
+ * @template E - The type of the error (must extend BaseError)
576
+ *
577
+ * @example
578
+ * ```typescript
579
+ * async function fetchUser(id: string): AsyncResult<User, NetworkError> {
580
+ * return AsyncResult.wrap(async () => {
581
+ * const response = await fetch(`/api/users/${id}`);
582
+ * return await response.json();
583
+ * });
584
+ * }
585
+ *
586
+ * const result = fetchUser("123")
587
+ * .map(user => user.name)
588
+ * .map(name => name.toUpperCase());
589
+ *
590
+ * const value = await result.take();
591
+ * if (isErr(value)) return value;
592
+ * console.log(value); // "ALICE"
593
+ * ```
594
+ */
595
+ class AsyncResult {
596
+ constructor(promise) {
597
+ Object.defineProperty(this, "promise", {
598
+ enumerable: true,
599
+ configurable: true,
600
+ writable: true,
601
+ value: promise
602
+ });
603
+ }
604
+ /**
605
+ * Creates an AsyncResult from a Promise of Result.
606
+ *
607
+ * @template T - The type of the success value
608
+ * @template E - The type of the error
609
+ * @param promise - The promise that resolves to a Result
610
+ * @returns An AsyncResult wrapping the promise
611
+ *
612
+ * @example
613
+ * ```typescript
614
+ * const asyncResult = AsyncResult.from(fetchData());
615
+ * ```
616
+ */
617
+ static from(promise) {
618
+ return new AsyncResult(promise);
619
+ }
620
+ /**
621
+ * Creates a successful AsyncResult with the given value.
622
+ *
623
+ * @template T - The type of the Ok value
624
+ * @template E - The type of the error (defaults to never)
625
+ * @param value - The value to wrap in an Ok AsyncResult
626
+ * @returns AsyncResult in the Ok state
627
+ *
628
+ * @example
629
+ * ```typescript
630
+ * const result = AsyncResult.ok(42);
631
+ * const value = await result.take();
632
+ * console.log(value); // 42
633
+ * ```
634
+ */
635
+ static ok(value) {
636
+ return new AsyncResult(Promise.resolve(Result.ok(value)));
637
+ }
638
+ /**
639
+ * Creates a failed AsyncResult with the given error.
640
+ *
641
+ * @template E - The type of the error
642
+ * @template T - The type of the Ok value (defaults to never)
643
+ * @param error - The error to wrap in an Err AsyncResult
644
+ * @returns AsyncResult in the Err state
645
+ *
646
+ * @example
647
+ * ```typescript
648
+ * const result = AsyncResult.err(new ValidationError("Invalid input"));
649
+ * const value = await result.take();
650
+ * if (Result.isErr(value)) {
651
+ * console.error(value.error.message); // "Invalid input"
652
+ * }
653
+ * ```
654
+ */
655
+ static err(error) {
656
+ return new AsyncResult(Promise.resolve(Result.err(error)));
657
+ }
658
+ /**
659
+ * Creates an AsyncResult from a Result, AsyncResult, or Promise<Result>.
660
+ *
661
+ * This is the key method for working with MaybeAsync types - it normalizes
662
+ * both synchronous Results and asynchronous AsyncResults into AsyncResults.
663
+ *
664
+ * @template T - The type of the success value
665
+ * @template E - The type of the error
666
+ * @param value - A Result, AsyncResult, or Promise<Result>
667
+ * @returns An AsyncResult
668
+ *
669
+ * @example
670
+ * ```typescript
671
+ * const asyncResult = AsyncResult.lift(Result.ok(42));
672
+ * const asyncResult2 = AsyncResult.lift(Promise.resolve(Result.ok(42)));
673
+ * const asyncResult3 = AsyncResult.lift(existingAsyncResult); // Pass-through
674
+ * ```
675
+ */
676
+ static lift(value) {
677
+ if (value instanceof AsyncResult) {
678
+ return value; // Already an AsyncResult, just return it
679
+ }
680
+ if (value instanceof Promise) {
681
+ return new AsyncResult(value);
682
+ }
683
+ return new AsyncResult(Promise.resolve(value));
684
+ }
685
+ /**
686
+ * Wraps an async function that might throw into an AsyncResult.
687
+ *
688
+ * Catches any exceptions and wraps them in UnexpectedError.
689
+ *
690
+ * @template T - The type of the return value
691
+ * @param fn - Async function that might throw
692
+ * @param context - Optional context to add to the error
693
+ * @returns AsyncResult with the return value or UnexpectedError
694
+ *
695
+ * @example
696
+ * ```typescript
697
+ * const user = AsyncResult.try(async () => {
698
+ * const response = await fetch("/api/user");
699
+ * return await response.json();
700
+ * });
701
+ *
702
+ * const value = await user.take();
703
+ * if (isErr(value)) {
704
+ * console.error("Fetch failed:", value.error);
705
+ * return;
706
+ * }
707
+ * console.log(value); // user object
708
+ * ```
709
+ */
710
+ static try(fn, context) {
711
+ return new AsyncResult((async () => {
712
+ try {
713
+ const value = await fn();
714
+ return Result.ok(value);
715
+ }
716
+ catch (cause) {
717
+ return Result.err(new error_js_1.UnexpectedError({ cause }).withContext(context));
718
+ }
719
+ })());
720
+ }
721
+ /**
722
+ * Combines multiple AsyncResults into a single AsyncResult containing an array.
723
+ *
724
+ * If all Results are Ok, returns Ok with an array of all values.
725
+ * If any Result is Err, returns the first Err encountered.
726
+ *
727
+ * @template T - The type of the Ok values
728
+ * @template E - The type of the errors
729
+ * @param results - Array of AsyncResults or Promises to combine
730
+ * @returns AsyncResult with array of values, or the first Err
731
+ *
732
+ * @example
733
+ * ```typescript
734
+ * const users = await AsyncResult.all([
735
+ * fetchUser("1"),
736
+ * fetchUser("2"),
737
+ * fetchUser("3")
738
+ * ]).take();
739
+ *
740
+ * if (Result.isErr(users)) {
741
+ * console.error("Failed to fetch users");
742
+ * } else {
743
+ * console.log(users); // [user1, user2, user3]
744
+ * }
745
+ * ```
746
+ */
747
+ static all(results) {
748
+ return new AsyncResult((async () => {
749
+ const resolvedResults = await Promise.all(results.map(async (r) => {
750
+ if (r instanceof AsyncResult) {
751
+ const res = await r;
752
+ return res;
753
+ }
754
+ return r;
755
+ }));
756
+ const values = [];
757
+ for (const result of resolvedResults) {
758
+ if (result.isErr()) {
759
+ return result;
760
+ }
761
+ const resultValue = result._unsafeValue();
762
+ if (resultValue.success) {
763
+ values.push(resultValue.value);
764
+ }
765
+ }
766
+ return Result.ok(values);
767
+ })());
768
+ }
769
+ /**
770
+ * Returns the first Ok result from an array of AsyncResults.
771
+ *
772
+ * If any Result is Ok, returns that Ok result.
773
+ * If all Results are Err, returns the last Err.
774
+ *
775
+ * @template T - The type of the Ok values
776
+ * @template E - The type of the errors
777
+ * @param results - Array of AsyncResults or Promises to check
778
+ * @returns AsyncResult with the first Ok, or the last Err
779
+ *
780
+ * @example
781
+ * ```typescript
782
+ * const data = await AsyncResult.any([
783
+ * fetchFromPrimary(),
784
+ * fetchFromSecondary(),
785
+ * fetchFromBackup()
786
+ * ]).take();
787
+ *
788
+ * if (Result.isErr(data)) {
789
+ * console.error("All sources failed");
790
+ * } else {
791
+ * console.log(data); // First successful result
792
+ * }
793
+ * ```
794
+ */
795
+ static any(results) {
796
+ return new AsyncResult((async () => {
797
+ const resolvedResults = await Promise.all(results.map(async (r) => {
798
+ if (r instanceof AsyncResult) {
799
+ const res = await r;
800
+ return res;
801
+ }
802
+ return r;
803
+ }));
804
+ for (const result of resolvedResults) {
805
+ if (result.isOk()) {
806
+ return result;
807
+ }
808
+ }
809
+ return resolvedResults[resolvedResults.length - 1];
810
+ })());
811
+ }
812
+ /**
813
+ * Implements PromiseLike to make AsyncResult awaitable.
814
+ *
815
+ * @template TResult1 - The type when fulfilled
816
+ * @template TResult2 - The type when rejected
817
+ * @param onfulfilled - Callback for when the promise is fulfilled
818
+ * @param onrejected - Callback for when the promise is rejected
819
+ * @returns A Promise of the result
820
+ */
821
+ then(onfulfilled, onrejected) {
822
+ return this.promise.then(onfulfilled, onrejected);
823
+ }
824
+ /**
825
+ * Transforms the Ok value using a mapper function, leaving Err untouched.
826
+ *
827
+ * @template U - The type of the transformed value
828
+ * @param fn - Function to transform the Ok value
829
+ * @returns A new AsyncResult with the transformed value
830
+ *
831
+ * @example
832
+ * ```typescript
833
+ * const result = fetchUser("123")
834
+ * .map(user => user.name)
835
+ * .map(name => name.toUpperCase());
836
+ * ```
837
+ */
838
+ map(fn) {
839
+ return new AsyncResult(this.promise.then((result) => result.map(fn)));
840
+ }
841
+ /**
842
+ * Transforms the Err value using a mapper function, leaving Ok untouched.
843
+ *
844
+ * @template F - The type of the transformed error
845
+ * @param fn - Function to transform the Err value
846
+ * @returns A new AsyncResult with the transformed error
847
+ *
848
+ * @example
849
+ * ```typescript
850
+ * const result = fetchUser("123")
851
+ * .mapErr(e => new NetworkError({ cause: e }));
852
+ * ```
853
+ */
854
+ mapErr(fn) {
855
+ return new AsyncResult(this.promise.then((result) => result.mapErr(fn)));
856
+ }
857
+ /**
858
+ * Chains operations that return Results.
859
+ *
860
+ * @template R - The Result type returned by the function
861
+ * @param fn - Function that takes the Ok value and returns a Result, AsyncResult, or Promise
862
+ * @returns A new AsyncResult from the chained operation
863
+ *
864
+ * @example
865
+ * ```typescript
866
+ * const result = fetchUser("123")
867
+ * .andThen(user => fetchPermissions(user.id));
868
+ * ```
869
+ */
870
+ andThen(fn) {
871
+ return new AsyncResult(this.promise.then(async (result) => {
872
+ const resultValue = result._unsafeValue();
873
+ if (isErrValue(resultValue)) {
874
+ return Result.err(resultValue.error);
875
+ }
876
+ const nextResult = fn(resultValue.value);
877
+ if (nextResult instanceof AsyncResult) {
878
+ return await nextResult;
879
+ }
880
+ return await nextResult;
881
+ }));
882
+ }
883
+ /**
884
+ * Extracts the value from Ok or returns the Err for early returns.
885
+ *
886
+ * This is the async version of Result.take(). It returns a Promise that
887
+ * resolves to either the unwrapped value T or an Err Result.
888
+ *
889
+ * @returns Promise of the unwrapped value or Err Result
890
+ *
891
+ * @example
892
+ * ```typescript
893
+ * async function processUser(id: string): Promise<Result<string, AppError>> {
894
+ * const user = await fetchUser(id).take();
895
+ * if (isErr(user)) return user;
896
+ *
897
+ * const perms = await fetchPermissions(user.id).take();
898
+ * if (isErr(perms)) return perms;
899
+ *
900
+ * return Result.ok(perms.join(", "));
901
+ * }
902
+ * ```
903
+ */
904
+ async take() {
905
+ const result = await this.promise;
906
+ return result.take();
907
+ }
908
+ /**
909
+ * Adds context to an Err result for early returns.
910
+ * Async version - can be chained before take().
911
+ *
912
+ * @param message - Context message describing the operation that failed
913
+ * @param extra - Optional additional context data
914
+ * @returns AsyncResult with context added to any error
915
+ *
916
+ * @example
917
+ * ```typescript
918
+ * const user = await fetchUser(id).context("failed to fetch user").take();
919
+ * if (isErr(user)) return user;
920
+ * ```
921
+ */
922
+ context(message, extra) {
923
+ return new AsyncResult(this.promise.then((result) => {
924
+ result.context(message, extra);
925
+ return result;
926
+ }));
927
+ }
928
+ /**
929
+ * Pattern matching for async Results.
930
+ *
931
+ * @template U - The type of the return value
932
+ * @param pattern - Object with ok and err handler functions
933
+ * @returns Promise of the result from calling either handler
934
+ *
935
+ * @example
936
+ * ```typescript
937
+ * const message = await fetchUser("123").match({
938
+ * ok: (user) => `Welcome, ${user.name}`,
939
+ * err: (error) => `Error: ${error.message}`
940
+ * });
941
+ * ```
942
+ */
943
+ async match(pattern) {
944
+ const result = await this.promise;
945
+ return result.match(pattern);
946
+ }
947
+ /**
948
+ * Returns the Ok value or a default value if Err.
949
+ *
950
+ * @template U - The type of the default value
951
+ * @param defaultValue - The value to return if the result is Err
952
+ * @returns Promise of the Ok value or the default value
953
+ */
954
+ async unwrapOr(defaultValue) {
955
+ const result = await this.promise;
956
+ return result.unwrapOr(defaultValue);
957
+ }
958
+ /**
959
+ * Returns the Ok value or computes a default from the error.
960
+ *
961
+ * @template U - The type of the default value
962
+ * @param fn - Function to compute the default value from the error
963
+ * @returns Promise of the Ok value or the computed default value
964
+ */
965
+ async unwrapOrElse(fn) {
966
+ const result = await this.promise;
967
+ return result.unwrapOrElse(fn);
968
+ }
969
+ /**
970
+ * Returns this result if Ok, otherwise returns the fallback.
971
+ *
972
+ * @template U - The type of the fallback Ok value
973
+ * @param other - The fallback Result or AsyncResult
974
+ * @returns AsyncResult of this or the fallback
975
+ */
976
+ or(other) {
977
+ return new AsyncResult(this.promise.then(async (result) => {
978
+ if (result.isOk()) {
979
+ return result;
980
+ }
981
+ if (other instanceof AsyncResult) {
982
+ return (await other);
983
+ }
984
+ return (await other);
985
+ }));
986
+ }
987
+ /**
988
+ * Returns this result if Ok, otherwise computes a fallback from the error.
989
+ *
990
+ * @template R - The Result or AsyncResult type returned by the fallback function
991
+ * @param fn - Function to compute a fallback Result from the error
992
+ * @returns AsyncResult of this or the computed fallback
993
+ */
994
+ orElse(fn) {
995
+ return new AsyncResult(this.promise.then(async (result) => {
996
+ const resultValue = result._unsafeValue();
997
+ if (isOkValue(resultValue)) {
998
+ return Result.ok(resultValue.value);
999
+ }
1000
+ const nextResult = fn(resultValue.error);
1001
+ if (nextResult instanceof AsyncResult) {
1002
+ return await nextResult;
1003
+ }
1004
+ return await nextResult;
1005
+ }));
1006
+ }
1007
+ /**
1008
+ * Performs a side effect on the Ok value without changing the result.
1009
+ *
1010
+ * @param fn - Function to call with the Ok value (if Ok)
1011
+ * @returns This AsyncResult, unchanged
1012
+ */
1013
+ inspect(fn) {
1014
+ return new AsyncResult(this.promise.then(async (result) => {
1015
+ const resultValue = result._unsafeValue();
1016
+ if (isOkValue(resultValue)) {
1017
+ await fn(resultValue.value);
1018
+ }
1019
+ return result;
1020
+ }));
1021
+ }
1022
+ /**
1023
+ * Performs a side effect on the Err value without changing the result.
1024
+ *
1025
+ * @param fn - Function to call with the error value (if Err)
1026
+ * @returns This AsyncResult, unchanged
1027
+ */
1028
+ inspectErr(fn) {
1029
+ return new AsyncResult(this.promise.then(async (result) => {
1030
+ const resultValue = result._unsafeValue();
1031
+ if (isErrValue(resultValue)) {
1032
+ await fn(resultValue.error);
1033
+ }
1034
+ return result;
1035
+ }));
1036
+ }
1037
+ }
1038
+ exports.AsyncResult = AsyncResult;