Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert the async Throwing Expression to a Result

According to Preserving the Results of a Throwing Expression documentation it is possible to convert throwing expression into Result type as follows:

let singleSample = Result { try UnreliableRandomGenerator().random() }

Is it also possible to convert async throwing expression into Result type like:

let singleSample = Result { try await UnreliableRandomGenerator().random() } 
like image 313
softshipper Avatar asked Oct 16 '25 09:10

softshipper


1 Answers

There is no built-in way. You cannot pass an async closure to the init in stdlib today. It is pretty trivial to add (based on the stdlib code):

extension Result where Failure == Swift.Error {
  public init(catching body: () async throws -> Success) async {
    do {
      self = .success(try await body())
    } catch {
      self = .failure(error)
    }
  }
}

But note that this makes the init async, so your code would need to be:

let singleSample = await Result { try await UnreliableRandomGenerator().random() }
                   ^^^^^

This would of course have to be called in an async context.

It's possible that what you're trying to do is make a "Future" so you can pass it through synchronous contexts. That already exists. It's called Task:

let singleSample = Task { try await UnreliableRandomGenerator().random() }

When you want to resolve this into a Result, you can await its .result property:

let result = await singleSample.result

Or you can skip the Result, and access its value directly with try-value:

let result = try await singleSample.value

In many cases, Result isn't all that useful in the Structured Concurrency world. It's better to use try and await directly. But it can still be very helpful for storing in properties, passing through channels that allow errors, and interacting with non-structured code, so the .result property on Task is very welcome, and it's nice that it easily can convert to and from throws.

like image 117
Rob Napier Avatar answered Oct 18 '25 09:10

Rob Napier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!