question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to unwrap an async

See original GitHub issue

I just upgraded from Cvdm.ErrorHandling to this. In Cvdm.ErrorHandling, I could unwrap an Async<Result<User, string>> by using let! operator without piping into any of the AsyncResult or Result module conversion functions.

// tryGetUser = int -> Async<Result<User, string>>
let! myResult = tryGetUser(1) // returns a Result<User, string>

Now this fails because it’s trying to look at the result type. But in this case, I don’t want to fully unwrap it to be just a User – I actually want it to be a Result<User, string> so that I can do some custom logic for the error case.

Is there a way to do this? I’m looking through the AsyncResult module, but I don’t see anything relevant.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
JordanMarrcommented, Oct 9, 2020

Would you consider adding this helper function to the AsyncResult module?

module AsyncResult =
    /// Awaits a result type without resolving it.
    let awaitResult (f: Async<Result<_, _>>) =
        f |> Async.map Ok

// Usage
// tryGetUser = int -> Async<Result<User, string>>
let! myResult = tryGetUser(1) |> AsyncResult.awaitResult // returns a Result<User, string>
match myResult with
| Ok user -> printfn "User: %A" user
| Error err -> printfn "Error: %s" err
1reaction
TheAngryByrdcommented, Oct 8, 2020

For a more concrete example, you can an async CE inline and do something like this:

let doTheThing () = asyncResult {
    let! user = async {
        let! myResult = tryGetUser(1)
        match myResult with
        | Ok user -> user
        | Error msg -> //custom errorHandling
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - await await vs Unwrap()
@hatcyl Unwrap returns a new task that represents the inner task of a Task<Task> . It will be able to return such a...
Read more >
TaskExtensions.Unwrap Method (System.Threading.Tasks)
Unwrap (Task<Task>). Creates a proxy Task that represents the asynchronous operation of a Task<Task> (C#) or Task (Of Task) (Visual Basic).
Read more >
How to: Unwrap a Nested Task | Microsoft Learn
Unwrap the inner task. Task<string> t3 = DoWorkAsync().ContinueWith((s) => DoMoreWorkAsync()).Unwrap(); // Outputs "More work completed.
Read more >
Where should value unwrapping be performed? #15
In #8, we agreed that when you yield a promise: async function* g() { yield new Promise(...); } The promise should be unwrapped...
Read more >
Task.Unwrap() - A useful proxy to avoid inner Task inside a Task
The Task that is returned by 'Unwrap' includes all the cancelation token request and exception handling (the proxy is doing the job of ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found