Slide 12
Slide 12 text
SwiftͰͷasync/await
06
Function
func asyncFetchData() async throws -> Data {
let url = URL(string: "https://jsonplaceholder.typicode.com/todos/1")!
let request = URLRequest(url: url)
let (data, response) = try await URLSession.shared.data(for: request,
delegate: nil)
guard (response as? HTTPURLResponse)?.statusCode == 200 else {
throw NSError(domain: "error", code: -1, userInfo: nil)
}
return data
}