【问题标题】:Un-nest JavaScript Firestore Functions取消嵌套 JavaScript Firestore 函数
【发布时间】:2018-11-22 07:46:30
【问题描述】:

如何重写此代码以使其不嵌套?我还需要访问之前函数调用中获得的值。

return docRef2.doc(`/users_stripe/${context.params.userID}`).get()
            .then(snapshot => {
            console.log("augu", snapshot);

            return stripe.customers.createSource( jsonParser(snapshot._fieldsProto.id, "stringValue"),
                    { source: jsonParser(snap._fieldsProto.token, "stringValue") },
                        function(err, card) {
                            console.log("listen people", card);
                            return docRef2.doc(`/users_stripe/${context.params.userID}/ptypes/ptypes`)
                            .set(card);
                    });
            })

【问题讨论】:

    标签: javascript node.js promise google-cloud-functions


    【解决方案1】:

    不确定您的代码在这里做什么。我尝试编写了一个伪/示例代码,可能会给你一个想法。 我的代码没有经过检查,所以可能存在问题。

    let fun1 = () => {
        return new Promise((resolve, reject) => {
          docRef2.doc('/route').get().then(snapshot => {
            if( snapshot ) resolve(snapshot);
            else reject(snapshot);
          })
        })
      }
    
      let fun2 = (snapshot) => {
        return new Promies((resolve, reject)=>{
          stripe.customers.createSource(jsonParser(snapshot._fieldsProto.id, "stringValue"),
            { source: jsonParser(snap._fieldsProto.token, "stringValue") },
            function (err, card) {
              if (err) reject(false);// or whatever you wanna return
              else resolve(card);
            });
        })
      }
    
      async function fun(){
        let res1 = await fun1(); // Should contain snapshot
        let res2 = await fun2(res1); // Should contain card
        return docRef2.doc(`/users_stripe/${context.params.userID}/ptypes/ptypes`)
          .set(card);
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-24
      • 2010-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 2019-05-23
      • 2012-02-07
      相关资源
      最近更新 更多