如何处理多个错误,然后捕获JavaScript Promise?

ash*_*ngh 5 javascript error-handling promise ecmascript-6 bluebird

让我们做一个诺言

p
.then(f1)
.then(f2)
.then(f3)
.catch(f4)
Run Code Online (Sandbox Code Playgroud)

现在陷入困境,可以从f1,f2,f3甚至p拒绝抛出错误

现在应该处理f4(或catch)错误的正确方法是什么,因为上面抛出的错误可能是不同的类型,是否可以避免f4中的多个错误?

ale*_*mac 1

只需定义一个额外的catch回调:

p
.then(f1)
.then(f2)
.then(f3)
.catch(err => {
  if (/* is expected error */) {
    console.log(err);
    return;
  }
  throw new Error('Unexpected error');
})
.catch(err => ...)
Run Code Online (Sandbox Code Playgroud)