React Native DRY Javascript -


1) have specific implementation want adhere dry principles. structure of both methods same. wondering if way not repeat implementation:

addcardtoexistingcustomer(carddetail){     paymentutil.stripecreatecardtoken(carddetail).then((cardtokenresult)=>{       if(cardtokenresult.error){         console.log("there error card!");        } else {           paymentutil.addcardtoexistingcustomer(cardtokenresult.id).then((card) =>{             });         }     });   }      addcardtonewcustomer(carddetail){     this.stripecreatecardtoken(carddetail).then((cardtokenresult)=>{       if(cardtokenresult.error){         console.log("there error card!");        } else {         console.log("successfully created card token");         paymentutil.stripecreatecustomer(cardtokenresult.id)        }     });   } 

2) best way chain promises ? how should handle exceptions promises in chain aren't last element of chain.

say example promise getstripecustomerid rejected. how should handle the rejection.

addcardtoexistingcustomer(cardtokenresultid){     return this.getstripecustomerid(userdetail).then((customerid) => {             return  this.removeallexistingcards(userdetail).then(()=>{                     return  stripe.addcardtocustomer(cardtokenresultid,customerid);             });         });      },   getstripecustomerid(userdetail){     return firebaserestutil.getstripecustomer(userdetail.username)         .then((fbstripe) => (fbstripe.customerid));  }, 

1- way can better.. use promises chains work on data.

getcardtokenresult(carddetail) {   return paymentutil.stripecreatecardtoken(carddetail)     .then((cardtokenresult) => {       if(cardtokenresult.error){         return promise.reject('there error card!!');       } else {         return cardtokenresult;       }     })     .catch((error) => console.log(error)): }  addcardtoexistingcustomer(carddetail){   return getcardtokenresult(carddetail)     .then((cardtokenresult) => {       paymentutil.addcardtoexistingcustomer(cardtokenresult.id).then((card) =>{         //       });     }); }  addcardtonewcustomer(carddetail){   return getcardtokenresult(carddetail)     .then((cardtokenresult) => {       paymentutil.stripecreatecustomer(cardtokenresult.id);     }); } 

2- can use catch errors on promises chain. can code above. added catch cardtokenresult error. can continue same way on chain other errors.


Comments