HomeiOS Developmentios - Find out how to convert a perform with completion into...

ios – Find out how to convert a perform with completion into an async perform with return worth? (Swift)


You need to use withCheckedThrowingContinuation to wrap pre-Swift 5.5 asynchronous strategies in an async technique.

If you need your async model to return non-Optionally available Information, you should use the under implementation.

The important thing level is that continuation.resume should be referred to as from all code paths, in any other case your technique won’t ever end from the code paths which do not name continuation.resume.

enum MyError: Error {
  case missingData
}

func execute() async throws -> Information {
  strive await withCheckedThrowingContinuation { continuation in
    let dataTask = execute(success: { knowledge in
      guard let knowledge else {
        continuation.resume(throwing: MyError.missingData)
        return
      }
      continuation.resume(returning: knowledge)
    }, failure: { error in
      continuation.resume(throwing: error)
    })
    dataTask.resume()
  }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments