A Variant is a data structure (tagged union) that can be used to represent any other data type. You define the various potential types and the variant can only represent one of those types at any one time.
A good use for this, is representing parts of your React/Redux app state.
const Any = () => true
const Request = Variant({
Unloaded: [],
Loading: [],
Loaded: [Any],
Failed: [Error]
})
// ...
componentDidMount () {
setState({request: Request.Loading})
fetchSomething
.then((results) => setState({request: Request.Loaded(results)}))
.catch(e => setState({request: Request.Failed(e)}))
}
// ...
render () {
return Request.case({
Unloaded: () => 'Nothing to see here.',
Loading: () => 'Please be patient.',
Loaded: (data) => `Got this data: ${data}`,
Failed: (error) => `Sorry: ${error}`
})
}