Skip to content

Instantly share code, notes, and snippets.

@Siilwyn
Last active December 11, 2018 13:36
Show Gist options
  • Save Siilwyn/76ee78a8a5a5d15c6598cf62c50afd4a to your computer and use it in GitHub Desktop.
Save Siilwyn/76ee78a8a5a5d15c6598cf62c50afd4a to your computer and use it in GitHub Desktop.
Get status with multiple conditions: if-else chain vs reducing
const resultState = state => {
if (!inputMatchesResult(state)) {
return 'unsynced';
} else if (state.isCalculating) {
return 'loading';
} else if (!state.result.success) {
return 'error';
} else if (state.result.success) {
return 'success';
}
};
const resultState = state => (
[
['success', state.result.success],
['error', !state.result.success],
['loading', state.isCalculating],
['unsynced', !inputMatchesResult(state)],
]
.reduce(
(outcome, [state, condition]) => (condition ? state : outcome),
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment