Avatar
@swift-5.4.3 -Xfrontend -enable-experimental-concurrency // // main.swift // ConcurrencyPlayground // // Created by Max Desiatov on 07/01/2021. // import _Concurrency import Foundation import FoundationNetworking struct UnknownError: Error {} func download(url: URL) async throws -> Data { await try withUnsafeThrowingContinuation { c in let task = URLSession.shared.dataTask(with: url) { data, _, error in switch (data, error) { case let (_, error?): return c.resume(throwing: error) case let (data?, _): return c.resume(returning: data) case (nil, nil): c.resume(throwing: UnknownError()) } } task.resume() } } runAsyncAndBlock { print("task started") let data = await try! download(url: URL(string: "https://httpbin.org/uuid")!) print(String(data: data, encoding: .utf8)!) } print("end of main") (edited)