I know that Javascript's promises are technically neither functors nor monads in the sense of Haskell, because (among other things)
- they include a
bind
operation that falls back tomap
when a pure function is passed in (and thus has an ambiguous type) - both the
Promise
constructor andresolve
(akareturn
) join nested promises recursively
The first issue can easily be bypassed by always providing a function with the right type a -> Promise b
.
The second issue obviously violates the parametricity trait of parametric polymorphic functions, i.e. one cannot construct a m (m a)
structure. But what would this structure mean in the context of promises/asynchronous computations? I cannot think of a meaningful semantics for Promise (Promise a)
, where Promise
is a monad. So what do we lose? What are the implications of the recursive joining?
Provided we are pretty pragmatic (and that's what we should be when we're programming Javascript), can't we claim that a Promise
is a monad in Javascript if we take care of the edge cases?