Я использую redux-observable
вместе с isomorphic-fetch
для обработки http-запросов в моем приложении React
. Я предпочитаю эту комбинацию с использованием rjjs ' ajax
потому что я тестирую Jest
которая запускает тесты в Node.js
и хочу перехватить HTTP-запросы с помощью nock
. См. Этот связанный вопрос: используйте fetch вместо 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.
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)
}
}
Затем в эпосе:
// 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!
)
Таким образом, отказ от обещаний обрабатывается в Наблюдаемом, но не прямо!
Любые предложения, как избежать предупреждения (и возможного будущего завершения с ненулевым кодом выхода) в этом сценарии?
С этого момента каждый раз, когда у вас есть обещание, и вы звоните 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