Why are Promises Monads?

admin

Administrator
Staff member
I've been learning about functional programming and have come across Monads, Functors and Applicatives.

From my understanding the following definitions apply:

a) ( A=>B ) => C[A] => C | Functor

b) ( A=>C ) => C[A] => C | Monad

c) ( C[A=>B] ) => C[A] => C | Applicative

(reference: <a href="https://thedet.wordpress.com/2012/04/28/functors-monads-applicatives-can-be-so-simple/" rel="noreferrer">https://thedet.wordpress.com/2012/04/28/functors-monads-applicatives-can-be-so-simple/</a>)

Furthermore, I understand a Monad is a special case of a Functor. As in, it applies a function <strong>that returns a wrapped value</strong> to a wrapped value and returns a wrapped value.

When we use
Code:
Promise.then(func)
, we are passing the Promise(i.e. C[A]) a function which normally has signature
Code:
A =&gt; B
and return another Promise (i.e. C). So my thinking was that a Promise would only be a Functor and not a Monad as
Code:
func
returns B and not C.

However, googling I found out that a Promise is not only a Functor, but also a Monad. I wonder why, as
Code:
func
does not return a wrapped value C but just B. What am I missing?