Instantly share code, notes, and snippets.

Embed
What would you like to do?
public extension RequestType {
public func execute (
dispatcher: NetworkDispatcher = URLSessionNetworkDispatcher.instance,
onSuccess: @escaping (ResponseType) -> Void,
onError: @escaping (Error) -> Void
) {
dispatcher.dispatch(
request: self.data,
onSuccess: { (responseData: Data) in
do {
let jsonDecoder = JSONDecoder()
let result = try jsonDecoder.decode(ResponseType.self, from: responseData)
DispatchQueue.main.async {
onSuccess(result)
}
} catch let error {
DispatchQueue.main.async {
onError(error)
}
}
},
onError: { (error: Error) in
DispatchQueue.main.async {
onError(error)
}
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment