Javascript 承诺:如何处理承诺链中的错误

Javascript 承诺:如何处理承诺链中的错误,javascript,promise,Javascript,Promise,我目前正在将Firebase数据库中的一些承诺与StripeAPI中的一些其他承诺链接起来。我想知道处理以下情况的最佳方法是什么: firebase.database().ref('orders').push(**a new order object**) .then((snap) => { //I get back the saved object from the database, and in particular its unique Id

我目前正在将Firebase数据库中的一些承诺与StripeAPI中的一些其他承诺链接起来。我想知道处理以下情况的最佳方法是什么:

   firebase.database().ref('orders').push(**a new order object**)
      .then((snap) => {
        //I get back the saved object from the database, and in particular its unique Id
        orderId = snap.key;
        //I then create a stripe Source
        return stripe.createSource({
          type: '....',
          amount: ...,
          currency: '....',
          ...
          metadata: {
            orderId: orderId
          }
        })
      })
      .then((result) => {
        //here is my problem!! 
        //the result of the stripe source creation is either a
        // a result.source -i.e. success- or a result.error 
        if (result.source) {
          //here I can continue the flow and return a value
        } else if (result.error) {
          // But here, ideally I should somehow emit an error that should be catched by the catch below. 
          //How to do that the correct way??
        }
      })
      .catch((err) => {

      })
我愿意这样做

.then(r => r.source ? doSomethingWith(r.source)
                    : Promise.reject(r.error))
.catch(handleError);

你可以扔掉它,也可以退回一个被拒绝的承诺。抛出要简单得多。只需使用
throwresult.error
将使承诺链被拒绝,而您的下一个
.catch()
将捕获错误。