У меня есть следующая цепочка обещаний:
return fetch(request)
.then(checkStatus)
.then(response => response.json())
.then(json => ({ response: json }))
.catch(error => ({ error }))
Где checkstatus()
проверяет, был ли запрос успешным, и возвращает ошибку, если это не так. Эта ошибка будет перехвачена и возвращена. Но проблема в том, что я хочу добавить к ошибке как response.statusText
, так и результаты response.json()
. Проблема в том, что когда я разбираю его, я теряю исходный ответ в цепочке, так как мне нужно вернуть response.json()
, потому что это обещание.
Вот что сейчас делает checkstatus:
const checkStatus = response => {
if (response.ok) return response
const error = new Error('Response is not ok')
// this works because the response hasn't been parsed yet
if (response.statusText) error.message = response.statusText
// an error response from our api will include errors, but these are
// not available here since response.json() hasn't been called
if (response.errors) error.errors = response.errors
throw error
}
export default checkStatus
Как вернуть ошибку с error.message = response.statusText
и error.errors = response.json().errors
?
response.json()
, потому что это обещание — это именно то, о чем канонический вопрос. - person Bergi   schedule 08.01.2017