Skip to content
New issue

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 中 then 的 onRejected vs catch #19

Open
negativeentropy9 opened this issue Aug 18, 2019 · 0 comments
Open

promise 中 then 的 onRejected vs catch #19

negativeentropy9 opened this issue Aug 18, 2019 · 0 comments
Labels

Comments

@negativeentropy9
Copy link
Owner

negativeentropy9 commented Aug 18, 2019

promise 中 then 的 onRejected vs catch

Promise.prototype.catch === Promise.prototype.then(undefined, onRejected)

两者之间是等价的。

不能捕获 异步调用中的 throw

// 在异步函数中抛出的错误不会被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); // 不会执行
});

在resolve()后面抛出的错误会被忽略

如果Promise状态以及变成resolved,再抛出错误是无效的。因为Promise的状态一旦改变,就永久保持该状态,不会再变了。

// 在resolve()后面抛出的错误会被忽略
var p3 = new Promise(function(resolve, reject) {
  resolve();
  throw 'Silenced Exception!';
});

p3.catch(function(e) {
   console.log(e); // 不会执行
});

实际捕获最近的 promise 的报错

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

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant