是否可以组合多个相同的 catch 块?

Javascript失败者

我想知道在 es2015 中使用 promise 链时是否可以将多个相同的 catch 块重构为单个 catch 块。

请参阅以下承诺链(不管它做什么):

replace(replaceOptions(dirPath + '/' + jsName))
         .then((changes) => {
             fileModifiedPrint(changes);
             removeDocumentation(jsName, dirPath, dirName);
             replace(replaceHTMLID(dirPath + '/' + jsName))
                .then((changes) => {

                })
                .catch((err) => {
                    fileErrorPrint(jsName, dirPath, err, "write"); return;
                })
         })
         .catch((err) => {
             fileErrorPrint(jsName, dirPath, err, "write"); return;
         })

我可以将这两个 catch 块合二为一吗?

jfriend00

是否可以组合多个相同的 catch 块?

是的,如果您通过从.then()处理程序中返回内部承诺来链接承诺,然后让拒绝传播冒泡到单个.catch()块,则可以:

replace(replaceOptions(dirPath + '/' + jsName)).then((changes) => {
    fileModifiedPrint(changes);
    removeDocumentation(jsName, dirPath, dirName);

    // return this promise to chain it to the parent promise
    return replace(replaceHTMLID(dirPath + '/' + jsName)).then((changes) => {
        // do whatever you want here
    });
}).catch((err) => {
    // this will catch any rejection anywhere in the promise chain
    // that wasn't already caught by someone
    fileErrorPrint(jsName, dirPath, err, "write");
});

当您链接承诺时,被拒绝的承诺将传播回链,到达.catch()它遇到的链中的第一个处理程序。如果.catch()在顶层之前没有处理程序,那么它会一直到那个.catch(). 这允许您在本地处理拒绝并将其从链的其余部分“隐藏”,或者通过让它向上传播来拒绝整个链。您可以根据最适合您的特定情况的逻辑来实施任一类型的逻辑。


仅供参考,您也可以像这样.then()在内部取消嵌套replace()

replace(replaceOptions(dirPath + '/' + jsName)).then((changes) => {
    fileModifiedPrint(changes);
    removeDocumentation(jsName, dirPath, dirName);

    // return this promise to chain it to the parent promise
    return replace(replaceHTMLID(dirPath + '/' + jsName));
}).then((changes) => {
    // results of the second `replace()` call.
    // do whatever you want here
}).catch((err) => {
    // this will catch any rejection anywhere in the promise chain
    // that wasn't already caught by someone
    fileErrorPrint(jsName, dirPath, err, "write");
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Java中,是否可以从try catch块返回多个值?

对不同的Promise使用相同的.then()。catch()块

从 catch 块抛出多个异常的最佳方法

Java RuntimeException捕获多个catch块

java catch块导致多个输出

在try catch块中将块用于IDisposable是否有问题?

是否需要 try-catch 块?

关于RAII,C ++`try` /`catch`块是否与其他块相同?

如果有多个 catch 块,为什么在 catch 块中没有捕获 RuntimeException?

我可以将多个try块与一个catch块一起使用吗

如何处理具有相同catch块的数字?

声明try catch块

Java Try Catch块

异步函数中有多个try-catch块

在try catch块中有多个fs.createReadStream

异常处理中多个“ catch”块的用途是什么

如何证明父异常的 Catch 块也可以处理子类

我可以避免这样麻烦的try ... catch块吗

嵌套的try异常是否将被外部catch块捕获

是否有嵌套的try / catch块的首选项?

在catch块中引发异常-是否会再次捕获?

PHP 异常 try/catch 块是否应该特定于异常?

如何测试Promise catch块是否设置了this.error

我可以利用多个try catch块引发多个错误或异常

猫鼬.catch块在多个地方都相同。如何避免这种情况?

在try / catch块上如何遵循IntelliJ-IDEA的建议“ catch分支相同”?

try catch块中的“ when”关键字是否与if语句相同?

如果为 Promise 中的“then”块提供了“onRejected”函数,是否不会执行 catch 块?

当发生异常时,可以暂停我的try块,执行catch块,然后再次恢复我的try块