Javascript 承诺:then vs then + catch

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33278280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:53:50  来源:igfitidea点击:

Promise : then vs then + catch

javascriptpromise

提问by Magus

Is there any difference between the 2 followings codes ?

以下2个代码之间有什么区别吗?

myPromise.then(function() {
    console.log('success');
}).catch(function() {
    console.log('error');
});

myPromise.then(function() {
    console.log('success');
}, function() {
    console.log('error');
});

I know thenand catchreturns new promises resolved or rejected with the value returns in the callback. But i see the 2 codes around the web and i'm curious about the real differences between the 2 codes.

我知道thencatch返回已解决或拒绝的新承诺,并在回调中返回值。但是我在网上看到了 2 个代码,我很好奇这 2 个代码之间的真正区别。

回答by fuyushimoya

In your current code, they act the same because console.log('success');will not fail.

在您当前的代码中,它们的行为相同,因为console.log('success');不会失败。

However, if you write something like this...

但是,如果你写这样的东西......

myPromise.then(function() {
   // Some error may happen
   throw('An exception that would be caught');
}).catch(function() {
    console.log('error');
});
// Is the same as this, the errHandle tries to catch any unhandled error
// from previous result.
myPromise.then(func, null).then(null, errHandle);


myPromise.then(function() {
   // Some error may happen
   throw('An unhandled exception.');
}, function() {
    // This won't log the error if it happens in the 
    // some error may happen block.
    console.log('error');
});
// Is the same as this, the errHandle will handle errors from previous result,
// but it won't handle errs in func.
myPromise.then(func, errHandle)

The second form can't catch that error, while the first can.

第二种形式不能捕捉到那个错误,而第一种可以。

Snippet for test:

测试片段:

// An function that may error and throw exception.
function funcThatThrows(test) {
  throw(`oops - error in test ${test}`);
}
function errHandler(exception) {
  console.log('We got an exception: ', exception);
}
// Expect: We got an exception:  oops - error in test 1
Promise.resolve(1).then(funcThatThrows).catch(errHandler);
// Is the same as below, the errHandler tries to catch any unhandled error
// from previous result.
// Expect: We got an exception:  oops - error in test 2
Promise.resolve(2).then(funcThatThrows, null).then(null, errHandler);

// If put the function and handler in the same then, the exception won't be caught.
// Expect: Uncaught (in promise) oops - error in test 3
Promise.resolve(3).then(funcThatThrows, errHandler);

回答by jkris

I'm guessing it depends on how the Promise is implemented. As far as I know jQuery implements it in a different fashion.

我猜这取决于 Promise 的实现方式。据我所知 jQuery 以不同的方式实现它。

The second you gave seems to be the jQuery version.

您提供的第二个似乎是 jQuery 版本。