I have a quick question:
- I have a network request that returns
Observable<Result<String, RequestError>>
, let’s call itrequestToken
- if this request succeeds, I want to use the
String
(token) to do another request that returnsObservable<Result<NSDictionary, RequestError>>
, let’s call itrequestData
- when that second request comes back, I wanna merge the token into its dictionary
- in the end I wanna map from
Observable<Result<String, RequestError>>
toObservable<Result<NSDictionary, RequestError>>
How can I achieve that without multiple nested levels in my code?
This is what I have today:
requestToken()
.flatMap({ result -> Observable<Result<NSDictionary, RequestError>> in
switch result {
case .success(let token):
return requestData(token: token).map({ $0.map({ $0 + ["token": token] }) })
case .failure(let error):
return Observable.of(.failure(error))
}
})