【发布时间】:2018-07-24 12:06:28
【问题描述】:
我想知道在 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 块合二为一吗?
【问题讨论】:
-
我相信是这样,错误会冒泡到外部的 catch 语句中
-
你应该链接承诺(例如,
return replace(…,总是,对于每个承诺)。见stackoverflow.com/questions/22539815/…
标签: javascript syntax ecmascript-6 promise