【发布时间】:2017-04-14 07:15:50
【问题描述】:
你如何处理超出承诺的错误(例如下面的“新错误”)?
function testError() {
throw new Error("new error") // how to handle this?
var p123 = new Promise(function(resolve, reject) {
resolve(123)
});
return p123
};
testError().catch(err => {
return err; // code doesn't come here
})
.then(ok => {
console.log(ok)
});
【问题讨论】:
-
使用
try/catch -
你必须在 promise 函数中抛出错误, .catch() 才能工作。
标签: javascript promise es6-promise request-promise