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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745205091a4616555.html
评论列表(0条)