简体   繁体   English

解决承诺捕获块中

[英]Resolving inside a promise catch block

I found this in some code. 我在一些代码中发现了这一点。 Is there ever a good reason to do this? 是否有充分的理由这样做?

}).catch(() => {
  resolve();
});

I'm curious about whether there are any conceivable ways that this is a good thing to do, but in this case the code I'm reviewing is: 我很好奇是否有任何可行的方法可以做到这一点,但是在这种情况下,我正在检查的代码是:

function checkExtendedPackage(config, packagePath) {
  return new Promise((resolve, reject) => {
    extend.check(packagePath)
      .then(() => {
        extend.validate(packagePath, config.allowExtends, config.scope)
          .then(packageToExtend => {
            showOutput.log([{
              type: 'success',
              description: 'validating',
              message: packageToExtend
            }]);
            resolve();
          }).catch(err => {
            reject(err);
          });
      }).catch(() => {
        resolve();
      });
  });
}

As you seem to have surmised, that's just poorly written code. 您似乎已经猜到了,那只是写得不好的代码。 There's nothing good about the part you initially pointed out and it is indeed an example of the explicit promise construction antipattern . 您最初指出的部分没有什么好处,它的确是显式promise构建反模式的一个示例。 It seems to have been written by someone who didn't really understand how to use promises. 它似乎是由一个不太了解如何使用诺言的人写的。

It can be improved like this: 可以这样改进:

function checkExtendedPackage(config, packagePath) {
    return extend.check(packagePath)
        .then(() => extend.validate(packagePath, config.allowExtends, config.scope)
            .then(packageToExtend => {
                showOutput.log([{
                    type: 'success',
                    description: 'validating',
                    message: packageToExtend
                }]);
            })
        , () => null);
}

As with the original code, this function will return a promise that: 与原始代码一样,此函数将返回以下承诺:

  • Will not reject if extend.check rejects (resolves to null in that case) 如果extend.check拒绝,则extend.check拒绝(在这种情况下解析为null
  • Will reject if extend.validate rejects (with the same error) 如果extend.validate拒绝(相同错误)将拒绝
  • Will execute showOutput.log with the necessary values if both succeed. 如果两个都成功,将使用必要的值执行showOutput.log

To reduce the nesting and complexity, I would suggest breaking this out into two functions: 为了减少嵌套和复杂性,我建议将其分为两个功能:

function validateAndLogExtendedPackage(config, packagePath) {
    return extend.validate(packagePath, config.allowExtends, config.scope)
        .then(packageToExtend => {
            showOutput.log([{
                type: 'success',
                description: 'validating',
                message: packageToExtend
            }]);
        });
}

function checkExtendedPackage(config, packagePath) {
    return extend.check(packagePath)
        .then(
            () => validateAndLogExtendedPackage(config, packagePath),
            () => null
        );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM