Avatar
omochimetaru 1/23/2021 1:21 PM
@swift-main -Xfrontend -enable-experimental-concurrency import Foundation import FoundationNetworking struct User: Identifiable, Codable { typealias ID = Int let id: ID var name: String var thumbnailURL: URL } func download(from url: URL) async throws -> Data { let data: Data = try Data(contentsOf: url) return data } func fetchUserWithThumbnail(for id: User.ID) async throws -> (user: User, thumbnail: Data) { let url: URL = URL(string: "https://koherent.org/async-await-challenge/api/user?id=\(id.description)")! let data = try await download(from: url) let decoder = JSONDecoder() let user = try decoder.decode(User.self, from: data) let thData = try await download(from: user.thumbnailURL) return (user: user, thumbnail: thData) } runAsyncAndBlock { do { let (user, thumbnail) = try await fetchUserWithThumbnail(for: 123) print(user.name, thumbnail.count) } catch { print(error) } }