Call function with completion handler

Solution 1:

Result should be Result<T, NetworkError>. No need to add modelToDecode to your method declaration. You can explicitly set the resulting type in your async call. Btw you should the completion handler as well if you fail to decode your data:

enum NetworkError: Error {
    case badURL, apiFailed, corruptedData
}

Your method should look like this:

func fetchDataAndDecode<T: Decodable>(url: String, completionHandler: @escaping (Result<T, NetworkError>) -> Void) {
    guard let url = URL(string: url) else {
        completionHandler(.failure(.badURL))
        return
    }

    AF.request(url, method: .get).validate().responseData { response in
        guard let data = response.data else {
            completionHandler(.failure(.apiFailed))
            return
        }

        do {
            let decodedData = try JSONDecoder().decode(T.self, from: data)
            DispatchQueue.main.async {
                completionHandler(.success(decodedData))
            }
        } catch {
            completionHandler(.failure(.corruptedData))
        }
    }
}

And when calling it you need to explicitly set the resulting type:

fetchDataAndDecode(url: "yourURL") { (result: Result<WhatEver, NetworkError>) in 
    // switch the result here
}