【发布时间】:2018-12-06 04:03:46
【问题描述】:
我有一个代码:
function () {
return new Promise((resolve, reject) => {
try {
fetch(URL, { method: METHOD, body: BODY })
.then((res) => res.json())
.then((json) => {
resolve(json);
})
.catch((res) => {
reject(res);
})
} catch (exception) {
reject(exception);
}
});
}
当服务器响应不是 json 我得到了
UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。
我知道 try-catch 不会捕获异步抛出。
我读过
Cannot catch UnhandledPromiseRejectionWarning in promise,
Try Catch unable to catch UnhandledPromiseRejectionWarning,
“UnhandledPromiseRejectionWarning” error even when catch is present
但我的问题没有答案。
【问题讨论】:
标签: javascript try-catch es6-promise