Skip to content
Discussion options

You must be logged in to vote

There is easy way to timeout async code in Swift, but you can do it manually, or make your own little operator to handle it:

case .someAction:
  state.connectionState = .connecting 
  return .run { send in 
    try await withThrowingTaskGroup(Void.self) { group in 
      group.addTask {
        let result1 = try await client.someOperation1()
        let result2 = try await client.someOparation2(result1)
        await send(.someActionResult(result2))
      }
      group.addTask {
        try await self.clock.sleep(for: .seconds(10))
        throw TimeoutError()
      }
      try await group.next()
      group.cancelAll()
    }
  }

Replies: 3 comments 10 replies

Comment options

You must be logged in to vote
3 replies
@andreyz
Comment options

@mbrandonw
Comment options

@andreyz
Comment options

Answer selected by tylerjames
Comment options

You must be logged in to vote
3 replies
@mbrandonw
Comment options

@tylerjames
Comment options

@mbrandonw
Comment options

Comment options

You must be logged in to vote
4 replies
@tylerjames
Comment options

@tylerjames
Comment options

@mbrandonw
Comment options

@tylerjames
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants