Last active
July 12, 2018 09:07
-
-
Save franciscoamado/97fb643186f8aab7888b6352e445ce70 to your computer and use it in GitHub Desktop.
Moya Extension with PromiseKit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PromiseKit | |
import Moya | |
public extension MoyaProvider { | |
public typealias PendingRequestPromise = (promise: Promise<Moya.Response>, cancellable: Cancellable) | |
public func request(target: Target, | |
queue: DispatchQueue? = nil, | |
progress: Moya.ProgressBlock? = nil) -> Promise<Moya.Response> { | |
return requestCancellable(target: target, | |
queue: queue, | |
progress: progress).promise | |
} | |
func requestCancellable(target: Target, | |
queue: DispatchQueue?, | |
progress: Moya.ProgressBlock? = nil) -> PendingRequestPromise { | |
let pending = Promise<Moya.Response>.pending() | |
let completion = promiseCompletion(fulfill: pending.fulfill, reject: pending.reject) | |
let cancellable = request(target, queue: queue, progress: progress, completion: completion) | |
return (pending.promise, cancellable) | |
} | |
private func promiseCompletion(fulfill: @escaping (Moya.Response) -> Void, | |
reject: @escaping (Swift.Error) -> Void) -> Moya.Completion { | |
return { result in | |
switch result { | |
case let .success(response): | |
fulfill(response) | |
case let .failure(error): | |
reject(error) | |
} | |
} | |
} | |
} |
Since PromiseKit 5.0, pending.fulfill is refactored to pending.resolver.fulfill and pending.reject is refactored to pending.resolver.reject
The above code is not working with PromiseKit 6.0. can you please update. Thanks in advance
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not working with Swift 4