我正在使用react-observable来开发react-native应用程序。
我有两个史诗,一个用于获取经纬度,另一个用于通过经纬度获取地址。
我的问题是,因为我想在一个史诗中做一件事,我如何使用第一个史诗的输出作为第二个史诗的输入?
const getLocationEpic = action$ =>
action$.ofType('GET_LOCATION')
.mergeMap(() =>
currentPosition$()
.map(data => ({
type: 'GET_LOCATION_SUCCESS',
data: {"latitude": data.coords.latitude, "longitude": data.coords.longitude}
}))
.catch(error => {
console.log("GET POSITION ERR", error);
return Observable.of({type: 'GET_POSITION_ERROR', error: error});
})
);
const getAddressEpic = action$ =>
action$.ofType('GET_ADDRESS')
.switchMap(action =>
ajax(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${latitude},${longitude}`)
.map(data => (data))
.do({
next: data => {
console.log("address data");
console.log(data);
}
})
.map(data => ({
type: 'GET_POSITION_SUCCESS', data: data.response.results[0].formatted_address
}))
.mergeMap(() =>
currentPosition$()
.map(data => ({
type: 'GET_ADDRESS_SUCCESS',
data: {"latitude": data.coords.latitude, "longitude": data.coords.longitude}
}))
.catch(error => {
console.log("GET_ADDRESS_ERROR", error);
return Observable.of({type: 'GET_ADDRESS_ERROR', error: error});
})
));
最佳答案
可能是这样的:
getLocationEpic(action)
.mergeMap( (action) => getAddressEpic(action) )
.subscribe( (data) => {
/* get the result from epic 1 to epic 2 */
})
详细信息:获取getLocationEpic
的结果,然后将其展平并将结果输入getAddressEpic
。订阅它以获取最终结果。
关于javascript - 在 react 可观察中使用史诗的一个输出作为史诗的另一个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45852079/