Frontend monads with consistent and beginner-friendly naming conventions.
> Frontend monads with consistent and beginner-friendly naming conventions.
* Monads let you map a function to a value.
* Monads are only ever in one state at a time. Each of their methods returns either the same monad or a new one in a different state.
Some almost monad things to help you understand:
* __Promises are almost monads__: They provide a consistent interface to structure the flow of async data
* __Promises are almost monads__: They can only ever be in one state at a time. Resolved, Rejected.
* __Arrays are almost monads__: They can always have a function mapped to their value, regardless of what they contain.
* __Array.map is almost monadic__: It provides a level of immutability by returning a new array with the values changed.
* Unit
* flatMap
* map
Some/None
Left/Right
Fetching/Error/Success
``js`
function unitExample() {
return test ? Some(value) : None();
}
The above function is consistent because it always returns a monad. However if test is true it will return aSome monad containing a value, otherwise it will return a None.
9/10 times you'd use the specific unit constructor for the monad you want but each monad does have a .unit method
Other terms: bind, chain
Fronads chooses flatMap/map because they pair nicely.
Map builds off both unit and Flatmap, and is essentially value => flatMap(Unit(value)).
Map is a convenient flatMap. It knows that you probably want the same monad again and so automatically creates the one you want. Letting you do things like:
```
Some(person).map(person => person.age)
The above statement will return a new some containing the persons age.