try? — try! ! do { let result = try divide(50.0, 0.0) print("The result is: \(result)") } catch let error { print("An error happened: \(error.localizedDescription)") }
-> [Product]) -> Void) { let request = URLRequest(url: URL(string:"https://mystore.com/products")!) URLSession.shared.dataTask(with: request) { (data, response, error) in if let error = error { handler { throw error } } if let data = data { handler { return try decoder.decode([Product].self, from: data) } } } } fetchProducts { (getResult) in do { let products = try getResult() print("Here is my list of products:") print(products) } catch let error { print("An error happened: \(error.localizedDescription)") } }
-> Void ) { let request = URLRequest(url: URL(string:"https://mystore.com/products")!) URLSession.shared.dataTask(with: request) { (data, response, error) in if let error = error { handler(.failure(.networkError(error))) } if let data = data { do { let products = try decoder.decode([Product].self, from: data) handler(.success(products)) } catch let error { handler(.failure(.parsingError(error))) } } } }
any success value using the given transformation. func map<NewSuccess>((Success) -> NewSuccess) -> Result<NewSuccess, Failure> // Returns a new result, mapping any failure value using the given transformation. func mapError<NewFailure>((Failure) -> NewFailure) -> Result<Success, NewFailure> // Returns a new result, mapping any success value using the given transformation and unwrapping the produced result. func flatMap<NewSuccess>((Success) -> Result<NewSuccess, Failure>) -> Result<NewSuccess, Failure> // Returns a new result, mapping any failure value using the given transformation and unwrapping the produced result. func flatMapError<NewFailure>((Failure) -> Result<Success, NewFailure>) -> Result<Success, NewFailure>
) { let request = URLRequest(url: URL(string:"https://mystore.com/products")!) URLSession.shared.dataTask(with: request) { (data, response, error) in if let error = error { handler(.failure(.networkError(error))) } if let data = data { handler(.success(data)) } } } fetchProducts { (result) in let data = result print(data) }