Last active
December 11, 2018 13:36
-
-
Save Siilwyn/76ee78a8a5a5d15c6598cf62c50afd4a to your computer and use it in GitHub Desktop.
Get status with multiple conditions: if-else chain vs reducing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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