Skip to content

Instantly share code, notes, and snippets.

@sethdavis512
Last active August 4, 2022 06:31
Show Gist options
  • Save sethdavis512/2ee26a29108b2ff420542b43341b6a95 to your computer and use it in GitHub Desktop.
Save sethdavis512/2ee26a29108b2ff420542b43341b6a95 to your computer and use it in GitHub Desktop.
export const composePromise = (...fns) => initialValue =>
fns.reduceRight((res, fn) => Promise.resolve(res).then(fn), initialValue);
// Individual Promises
const saveAppFee = () => saveFees(a, b, c);
const saveLnpFee = () => saveFees(x, y, z);
// Composed Promises
const saveAppAndLnp = composePromise(saveAppFee, saveLnpFee);
const saveApp = composePromise(saveAppFee);
const saveLnp = composePromise(saveLnpFee);
// Add logic to return proper set of promises.
const determineFeeCalls = (shouldCallA, shouldCallB) => {
if (shouldCallA && shouldCallB) {
return saveAppAndLnp;
} else if (shouldCallA) {
return saveApp;
} else if (shouldCallB) {
return saveLnp;
}
};
// Your condition here...
const shouldCallAppFee = true;
const shouldCallLnPFee = false;
// Call using currying
determineFeeCalls(shouldCallAppFee, shouldCallLnPFee)()
.then(() => {
history.push(`${Constants.BASE_PATH}/payment/license-summary-preview`);
})
.catch(err => {
hideSpinner();
// error handling here
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment