使用
.then
正确的做法是使用rxjs的
from
函数,如下所示
import 'rxjs';
import { FETCH_DATA, FETCH_DATA_FAIL } from './constants';
import { fetchData } from './actions';
import axios from 'axios';
import { Observable, from } from 'rxjs';
import { mergeMap, map } from 'rxjs/operators';
import { ofType } from 'redux-observable';
export const exampleEpic = action$ =>
action$.pipe(ofType(FETCH_DATA),
mergeMap(action =>
from(axios.get('https://jsonplaceholder.typicode.com/todos/1'))
.map(response => fetchData(response))
.catch(error => Observable.ofType(FETCH_DATA_FAIL)))
);
另外,请确保导入
.map
来自rxjs的运算符
更新:使用.pipe操作符的语法
export const exampleEpic = action$ =>
action$.pipe(
ofType(FETCH_DATA),
mergeMap(action => from(axios.get('https://jsonplaceholder.typicode.com/todos/1/'))
.pipe(
map(response => fetchData(response)),
catchError(() => of(fetchDataFailure()))
)
)
)