Using async call in boolean expression

I have to get a boolean result, which depends on 2 conditions. One of those conditions is costly and asynchronous. So I'd rather not check the second condition unless the first is true.

I have code that is written like this:

        let condition1: Bool = ...
        func condition2() async throws -> Bool {...}
        
        ...
        
        let result: Bool
        if (condition1) {
            result = try await condition2()
        } else {
            result = false
        }

        if result { ... }

but I'd like to be able to do something like this, which doesn't compile:

let result = condition1 && (try await condition2())
if result { ... }

The errors that I get are

  1. 'async' call in an autoclosure that does not support concurrency
  2. Operator can throw but expression is not marked with 'try'

Am I missing something? Is there a better way to write this code?

If it doesn't compile, what error do you get?

Sorry for not including that information. I updated the original post.

The errors that I get are

  1. 'async' call in an autoclosure that does not support concurrency
  2. Operator can throw but expression is not marked with 'try'

&& needs to be updated with reasync once that's supported. Until then you'll have to do it separately.

3 Likes
Terms of Service

Privacy Policy

Cookie Policy