We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Result<UIImage, Error>
// ✅ Completion handler를 이용한 비동기 처리 방식 func fetchThumbnail(for id: String, completion: @escaping (UIImage?, Error?) -> Void) { let request = thumbnailURLRequest(for: id) let task = URLSession.shared.dataTask(with: request) { data, response, error in if let error = error { completion(nil, error) // ✅ } else if (response as? HTTPURLResponse)?.statusCode != 200 { completion(nil, FetchError.badID) // ✅ } else { guard let image = UIImage(data: data!) else { completion(nil, FetchError.badImage) // ✅ return } image.prepareThumbnail(of: CGSize(width: 40, height: 40)) { thumbnail in guard let thumbnail = thumbnail else { completion(nil, FetchError.badImage) // ✅ return } completion(thumbnail, nil) // ✅ } } } task.resume() }
async
await
// ✅ Async Await을 이용한 비동기 처리 방식 func fetchThumbnail(for id: String) async throws -> UIImage { let request = thumbnailURLRequest(for: id) let (data, response) = try await URLSession.shared.data(for: request) guard (response as? HTTPURLResponse)?.statusCode == 200 else { throw FetchError.badID } let maybeImage = UIImage(data: data) guard let thumbnail = await maybeImage?.thumbnail else { throw FetchError.badImage } return thumbnail }
The text was updated successfully, but these errors were encountered:
samsung-ga
No branches or pull requests
Asychronous Programming
Asnyc / await 이란?
Completion handler의 단점
Result<UIImage, Error>
타입을 이용하여 좀 더 안전하게 작성이 가능하지만1. Async / await 등장
async
await
async
-. 함수의 매개변수 뒤이자, throws 키워드 앞에 선언
-. 비동기 태스크가 결과를 반환할 때까지 실행 일시 중지 가능
-. 컴파일러는 함수가 무조건 비동기 컨텍스트에서 호출됨을 보장
await
2. Async Await 장점
3. Dig In
await
키워드를 이용해서 비동기적으로 작동하게 함Adopting Async/await
The text was updated successfully, but these errors were encountered: