Skip to content

Commit 54fcc90

Browse files
committed
[Async Refactoring] Wrap code in a continuation if conversion doesn't yield reasonable results
If we are requested to convert a function to async, but the call in the function’s body that eventually calls the completion handler doesn’t have an async alternative, we are currently copying the call as-is, replacing any calls to the completion handler by placeholders. For example, ```swift func testDispatch(completionHandler: @escaping (Int) -> Void) { DispatchQueue.global.async { completionHandler(longSyncFunc()) } } ``` becomes ```swift func testDispatch() async -> Int { DispatchQueue.global.async { <#completionHandler#>(longSyncFunc()) } } ``` and ```swift func testUrlSession(completionHandler: @escaping (Data) -> Void) { let task = URLSession.shared.dataTask(with: request) { data, response, error in completion(data!) } task.resume() } ``` becomes ```swift func testUrlSession() async -> Data { let task = URLSession.shared.dataTask(with: request) { data, response, error in <#completion#>(data!) } task.resume() } ``` Both of these are better modelled using continuations. Thus, if we find an expression that contains a call to the completion handler and can’t be hoisted to an await statement, we are wrapping the rest of the current scope in a `withChecked(Throwing)Continuation`, producing the following results: ```swift func testDispatch() async -> Int { return await withCheckedContinuation { (continuation: CheckedContinuation<Int, Never>) in DispatchQueue.global.async { continuation.resume(returning: syncComputation()) } } } ``` and ```swift func testDataTask() async -> Int? return await withCheckedContinuation { (continuation: CheckedContinuation<Data, Never>) in let task = URLSession.shared.dataTask { data, response, error in continuation.resume(returning: data!) } task.resume() } } ``` I think both are much closer to what the developer is actually expecting. Resolves rdar://79304583
1 parent d0472e1 commit 54fcc90

File tree

3 files changed

+684
-72
lines changed

3 files changed

+684
-72
lines changed

0 commit comments

Comments
 (0)