mcm*_*der 3 javascript node.js reactjs redux redux-observable
我redux-observable与一起使用isomorphic-fetch来处理我的React应用程序中的http请求。与使用rxjs'相比ajax,我更喜欢这种组合,因为我正在测试Jest-可以在Node.js- 中运行测试,并希望使用来拦截http请求nock。参见以下相关问题:使用fetch代替redux-observable的ajax。
这就是问题所在:我感到UnhandledPromiseRejectionWarning恐惧:DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.因为我不是直接兑现我的诺言拒绝,而是将其留给了可观察者:
// apiModule.js
import fetch from 'isomorphic-fetch'
const api = {
getSomething: () => {
const request = fetch('http://some-api/')
.then(res => catchError(res)) // throwing an Error here if not response.ok
.then(res => res.json())
return Observable.from(request)
}
}
Run Code Online (Sandbox Code Playgroud)
然后在史诗中:
// myReduxModule.js
import {api} from './apiModule.js'
const getSomethingEpic = action$ =>
action$
.ofType(GET_SOMETHING)
.mergeMap(action =>
api
.getSomething()
.map(response => getSomethingSucceeded(response))
.catch(error => logError(error)) // error is handled here!
)
Run Code Online (Sandbox Code Playgroud)
因此,承诺拒绝是在Observable中处理的,而不是直接处理的!
有什么建议可以避免这种情况下的警告(并且将来可能会以非零退出代码终止)?
从现在开始,每当您有一个承诺并调用时then,还必须实现catch。同样在您的测试中。
const request = fetch('http://some-api/')
.then(res => catchError(res))
.then(res => res.json())
.catch(err => catchError(res)) // <= here you should implement the catch
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2158 次 |
| 最近记录: |