You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+78-14Lines changed: 78 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -685,16 +685,76 @@ See [Async support](#async-support) for more context.
685
685
686
686
Because it can be quite cumbersome to work with results that are wrapped in a promise, we provide an `AsyncResult` type that is essentially a regular promise that contains a `Result` type, along with most of the methods that are available on the regular `Result` type. This makes it easier to chain operations without having to assign the intermediate results to a variable or having to use `await` for each async operation.
687
687
688
-
There are of course plenty of scenarios where an async function returns a `Result` (`Promise<Result<*, *>>`). In these cases, you can use the `fromAsync` and `fromAsyncCatching` methods to convert the promise to an `AsyncResult`, and continue chaining operations:
688
+
There are of course plenty of scenarios where an async function (or method) returns a `Result` (`Promise<Result<*, *>>`). Although there nothing wrong with this per se, it can become a bit cumbersome to await each function call before you can perform any operations. You can use the `fromAsync` and `fromAsyncCatching` utility methods to make working with results in an async context more ergonomic and developer-friendly.
689
+
690
+
There are two approaches you can choose from: transform to an `AsyncResult`_directly at the source_, or let the _consuming code_ handle the conversion. Let's look at both approaches in more detail.
691
+
692
+
#### Transforming to an `AsyncResult` directly at the source
693
+
694
+
Before:
695
+
```ts
696
+
// Note the `async` keyword and that we return a `Promise` holding a `Result`
@@ -1336,15 +1396,19 @@ const result = Result.try(
1336
1396
); // Result<void, IOError>
1337
1397
```
1338
1398
1339
-
### Result.fromAsync(promise)
1399
+
### Result.fromAsync()
1340
1400
1341
-
Utility method to transform a Promise, that holds a literal value or
1342
-
a [`Result`](#result) or [`AsyncResult`](#asyncresult) instance, into an [`AsyncResult`](#asyncresult) instance. Useful when you want to immediately chain operations
1401
+
Utility method to:
1402
+
- transform a Promise, that holds a literal value or
1403
+
a [`Result`](#result) or [`AsyncResult`](#asyncresult) instance; or,
1404
+
- transform an async function
1405
+
into an [`AsyncResult`](#asyncresult) instance. Useful when you want to immediately chain operations
1343
1406
after calling an async function.
1344
1407
1345
1408
#### Parameters
1346
1409
1347
-
-`promise` a Promise that holds a literal value or a [`Result`](#result) or [`AsyncResult`](#asyncresult) instance.
1410
+
-`promise` a Promise that holds a literal value or a [`Result`](#result) or [`AsyncResult`](#asyncresult) instance. or,
1411
+
-`fn` an async callback funtion returning a literal value or a [`Result`](#result) or [`AsyncResult`](#asyncresult) instance.
1348
1412
1349
1413
**returns** a new [`AsyncResult`](#asyncresult) instance.
1350
1414
@@ -1366,9 +1430,9 @@ const result = (await someAsyncOperation()).map((value) => value 2); // Result<n
Similar to [`Result.fromAsync`](#resultfromasyncpromise) this method transforms a Promise into an [`AsyncResult`](#asyncresult) instance.
1435
+
Similar to [`Result.fromAsync`](#resultfromasync) this method transforms a Promise or async callback function into an [`AsyncResult`](#asyncresult) instance.
1372
1436
In addition, it catches any exceptions that might be thrown during the operation and encapsulates them in a failed result.
* Utility method to transform a Promise, that holds a literal value or
1670
1705
* a {@linkcode Result} or {@linkcode AsyncResult} instance, into an {@linkcode AsyncResult} instance. Useful when you want to immediately chain operations
@@ -1692,40 +1727,33 @@ export class Result<Value, Err> {
0 commit comments