admin管理员组文章数量:1415673
I am using redux-observable
together with isomorphic-fetch
to handle http requests in my React
app. I am favouring this bination over using rxjs' ajax
because I am testing with Jest
- which runs tests in Node.js
- and want to intercept http requests with nock
. See this related question: Use fetch instead of ajax with redux-observable.
So here is the problem: I get an UnhandledPromiseRejectionWarning
together with a scary: 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.
because I am not catching my promise rejections directly but rather leave that to the Observable:
// 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)
}
}
Then in the epic:
// 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!
)
So the promise rejection is handled in the Observable but not directly!
Any suggestions how to avoid the warning (and a possible future terminate with non-zero exit code) in this scenario?
I am using redux-observable
together with isomorphic-fetch
to handle http requests in my React
app. I am favouring this bination over using rxjs' ajax
because I am testing with Jest
- which runs tests in Node.js
- and want to intercept http requests with nock
. See this related question: Use fetch instead of ajax with redux-observable.
So here is the problem: I get an UnhandledPromiseRejectionWarning
together with a scary: 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.
because I am not catching my promise rejections directly but rather leave that to the Observable:
// 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)
}
}
Then in the epic:
// 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!
)
So the promise rejection is handled in the Observable but not directly!
Any suggestions how to avoid the warning (and a possible future terminate with non-zero exit code) in this scenario?
Share Improve this question asked Jun 8, 2017 at 9:06 mcmundermcmunder 4451 gold badge5 silver badges11 bronze badges1 Answer
Reset to default 6From now on, every time you have a promise and you call the then
, you must also implement the catch
. Also in your tests.
const request = fetch('http://some-api/')
.then(res => catchError(res))
.then(res => res.json())
.catch(err => catchError(res)) // <= here you should implement the catch
本文标签:
版权声明:本文标题:javascript - How to handle `UnhandledPromiseRejectionWarning` when using Observable.from(<Promise>) and catching e 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745205091a2647601.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论