We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Promise.prototype.catch === Promise.prototype.then(undefined, onRejected)
两者之间是等价的。
// 在异步函数中抛出的错误不会被catch捕获到 var p2 = new Promise(function(resolve, reject) { setTimeout(function() { throw 'Uncaught Exception!'; }, 1000); }); p2.catch(function(e) { console.log(e); // 不会执行 }); p2.then(undefined, function(e) { console.log(e); // 不会执行 });
如果Promise状态以及变成resolved,再抛出错误是无效的。因为Promise的状态一旦改变,就永久保持该状态,不会再变了。
// 在resolve()后面抛出的错误会被忽略 var p3 = new Promise(function(resolve, reject) { resolve(); throw 'Silenced Exception!'; }); p3.catch(function(e) { console.log(e); // 不会执行 });
Promise对象的错误具有冒泡性质,会一直向后传递,直到被捕获为止
Promise.reject('error').then((data) => { console.log('debug-data',data) }, e => { console.log('debug-then-fail', e); // debug-then-fail error }).catch(e => { console.log('debug-catch', e); }); Promise.reject('error').then((data) => { console.log('debug-data',data); }).catch(e => { console.log('debug-catch', e); // debug-catch error });
sandbox demo
The text was updated successfully, but these errors were encountered:
No branches or pull requests
promise 中 then 的 onRejected vs catch
两者之间是等价的。
不能捕获 异步调用中的 throw
在resolve()后面抛出的错误会被忽略
如果Promise状态以及变成resolved,再抛出错误是无效的。因为Promise的状态一旦改变,就永久保持该状态,不会再变了。
实际捕获最近的 promise 的报错
Promise对象的错误具有冒泡性质,会一直向后传递,直到被捕获为止
sandbox demo
参考
The text was updated successfully, but these errors were encountered: