0Pricing
Swift Academy · 课时

将 Result 衔接到 async/await

将基于回调的 Result API 转换为异步抛出函数。

将 Result 衔接到 async/await 是 CoddyKit 上的免费 Swift Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Swift Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Swift Academy 课程共包含 4 节课。

欢迎

基于回调的旧版 API 会返回 `Result`。现代代码使用异步/等待。两者之间的桥接是常见任务,Swift 为这两个方向都提供了简洁的模式。

withCheckedThrowingContinuation

```swift func fetchAsync(url: URL) async throws -> Data { try await withCheckedThrowingContinuation { cont in fetchCallback(url) { result in cont.resume(with: result) } } } ``` `cont.resume(with: result)` 可以直接接收 `Result`。

withCheckedContinuation(不抛出错误)

```swift func fetchAsync(url: URL) async -> Result { await withCheckedContinuation { cont in fetchCallback(url) { result in cont.resume(returning: result) } } } ```

将异步操作转换为结果类型

```swift func asResult(of operation: () async throws -> T) async -> Result { do { return .success(try await operation()) } catch { return .failure(error) } } let r = await asResult { try await fetch(url) } ```

异步上下文中的 Result.get()

```swift func process() async { let result: Result = await fetchAsync(url) do { let data = try result.get() handle(data) } catch { handle(error) } } ```

使用 Task { } 进行桥接

```swift func load() { Task { let data = try await fetchAsync(url) await MainActor.run { self.render(data) } } } ```

从回调创建 AsyncStream

```swift func events() -> AsyncStream { AsyncStream { cont in let token = subscribe { event in cont.yield(event) } cont.onTermination = { _ in unsubscribe(token) } } } ```

续体安全规则

• 只调用一次 `resume`——调用两次会产生未定义行为 • 函数返回后不要再调用 `resume` • 在调试版本中使用 `withCheckedContinuation`(可以检测错误);如有性能需要,可在发布版本中使用 `withUnsafeContinuation`

使用 MainActor 更新界面

```swift await MainActor.run { label.text = data.description } // or: @MainActor func updateUI(_ d: Data) { label.text = ... } await updateUI(data) ```

测试异步结果处理

```swift func testFetch() async throws { let result = await asResult { try await fetchAsync(url) } switch result { case .success(let d): XCTAssertFalse(d.isEmpty) case .failure(let e): XCTFail(e.localizedDescription) } } ```

快速检查

哪种延续类型会在调试版本中检测出重复恢复等误用?

回顾

主要要点: • `withCheckedThrowingContinuation` 将回调桥接到异步抛出流程 • `cont.resume(with: result)` 可直接接受结果值 • 使用辅助函数将 `async throws` 包装在 `Result` 中 • 只调用一次 `resume` • 使用 `MainActor.run { }` 从异步上下文安全地更新用户界面 课程完成!接下来学习 SwiftUI 基础。

常见问题解答

「将 Result 衔接到 async/await」课时是免费的吗?

是的 — 「将 Result 衔接到 async/await」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Swift Academy 课程的其余内容,请升级到 CoddyKit PRO。 Swift Academy 课程共包含 4 节课。

「将 Result 衔接到 async/await」这节课中我会学到什么?

将基于回调的 Result API 转换为异步抛出函数。 你通过在浏览器中直接运行的动手代码来练习 Swift Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Swift Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Swift Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「将 Result 衔接到 async/await」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Swift Academy 课中编写并运行代码吗?

能。每节 Swift Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Result 基础
  2. Result 上的 map 与 flatMap
  3. 使用 LocalizedError 自定义错误类型
  4. 将 Result 衔接到 async/await
← 返回 Swift Academy