I'm unable to get withCheckedThrowingContinuation
to compile when there’s no return type. For example:
public
func
write(toDevice inDeviceID: Int, atAddress inAddr: Int, value inVal: Float)
async
throws
{
try await withCheckedThrowingContinuation
// ^ Generic parameter 'T' could not be inferred
{ inCont in
self.workQ.async
{
do
{
self.deviceID = inDeviceID
try self.write(address: inAddr, value: inVal)
inCont.resume()
}
catch (let e)
{
inCont.resume(throwing: e)
}
}
}
}
A version that returns works just fine:
public
func
readRegister(address inAddr: Int, fromDevice inDeviceID: Int)
async
throws
-> UInt16
{
try await withCheckedThrowingContinuation
{ inCont in
self.workQ.async
{
do
{
self.deviceID = inDeviceID
let r = try self.readRegister(address: inAddr)
inCont.resume(returning: r)
}
catch (let e)
{
inCont.resume(throwing: e)
}
}
}
}
I thought I would add another example
extension Something {
public func sync() async throws {
// The swift compiler demands that we be explicit where there is no return type.
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) -> Void in
self.sync { result in
switch result {
case .success(let entry):
continuation.resume(with: .success(entry))
case .failure(let error):
continuation.resume(with: .failure(error))
}
}
}
}
}
UPDATE: Xcode can now wrap your closure based function with async/await continuations.