I'm attempting an API Request, however I'm receiving an error
Cannot assign value of type 'Promise<T>' to type 'Promise<_>?'
I have no idea where 'Promise<_>?' is arising.
Here's a look at the code: throwing error at 'as! Promise'
class Cache<T> {
private let defaultMaxCacheAge: TimeInterval
private let defaultMinCacheAge: TimeInterval
private let endpoint: () -> Promise<T>
private let cached = CachedValue<T>()
private var pending: Promise<T>?
// Always makes API request, without regard to cache
func fresh() -> Promise<T> {
// Limit identical API requests to one at a time
if pending == nil {
pending = endpoint().then { freshValue in
self.cached.value = freshValue
return .value(freshValue)
}.ensure {
self.pending = nil
} as! Promise<T>
}
return pending!
}
//...More code...
}
And Cache's init
init(defaultMaxCacheAge: TimeInterval = .OneHour, defaultMinCacheAge: TimeInterval = .OneMinute, endpoint: @escaping () -> Promise<T>) {
self.endpoint = endpoint
self.defaultMaxCacheAge = defaultMaxCacheAge
self.defaultMinCacheAge = defaultMinCacheAge
}
And CachedValue
class CachedValue<T> {
var date = NSDate.distantPast
var value: T? { didSet { date = NSDate() as Date } }
}