Skip to content

Instantly share code, notes, and snippets.

@GonchuB
Last active January 2, 2020 13:36
Show Gist options
  • Save GonchuB/37ceed953922e2652a710bf81978cd37 to your computer and use it in GitHub Desktop.
Save GonchuB/37ceed953922e2652a710bf81978cd37 to your computer and use it in GitHub Desktop.
// Option 1: Duplicating the function
async function payWithPayPal(payload) {
setLoading(true);
await apis.paypal(payload);
trackWithAnalytics({ method: 'paypal', amount: payload.amount });
setLoading(false);
}
async function payWithCreditCard(payload) {
setLoading(true);
await apis.creditcard(payload);
trackWithAnalytics({ method: 'paypal', amount: payload.amount });
setLoading(false);
}
// Option 2: "Creating an abstraction" to "pay" with any payment method
const paymentMethods = ['paypal', 'creditcard'];
async function pay(paymentMethod, payload) {
setLoading(true);
await apis[paymentMethod](payload);
trackWithAnalytics({ method: paymentMethod, amount: payload.amount });
setLoading(false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment