我有一个辅助函数,用于将 fetch
与 CouchDB 一起使用,其结尾为:
...
return fetch(...)
.then(resp => resp.ok ? resp.json() : Promise.reject(resp))
.then(json => json.error ? Promise.reject(json) : json)
当我在其他地方使用它时,我的印象是我可以.catch
那些明确的拒绝:
above_function(its_options)
.then(do_something)
.catch(err => do_something_with_the_json_error_rejection_or_resp_not_ok_rejection_or_the_above(err))
但是唉,我似乎无法掌握拒绝的信息。 我遇到的具体错误是 HTTP 401 响应。
什么给出了?
(请注意,.then
中存在隐式 ES6 return
)
最佳答案
function test() {
return new Promise((resolve, reject) => {
return reject('rejected')
})
}
test().then(function() {
//here when you resolve
})
.catch(function(rej) {
//here when you reject the promise
console.log(rej);
});
关于javascript - 如何.catch a Promise.reject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46005954/