You are already using monad and just don't know it. Simply using
The definition of a monand comes in 2 parts. First is to create a monad, another is to map a monad to another.
We "create the Maybe monad" by setting a variable of type
Maybe<number> with either a
undefined. We "map the Maybe monad" with
We can write rewrite
maybeDouble to a more general form.
What is the point of all this? We can see by using
mapMaybe, we don't need to do null-checks explicitly anymore. The null-check is implemented once in
mapMaybe and never again.
Another reason is to avoid the Pyramid of doom. Code without monads can get too nested.
But we can flatten the code with monads.
Once one understands monad is simply two operations (create and map), one can see monads in other parts of the code.
Promiseis a monad
async/await is just syntanic sugar over
Promises, they too are monads. It is just less obvious at first.
Promise is mapped using the
async/await keywords. Since
doubleFoo is an
async function, it returns a
async/await made it easy to access the resolved value.
Do you want to to learn how to see more than just monads? You're in luck, Battlefy is hiring.