Instead of creating a promise monad or merging the promise and monad interfaces into a larger, more confusing interface, I created a promiseT monad transformer, along the lines of the Haskell stateT monad transformer. Haskell does not have a State monad, but rather uses stateT to add stateful behavior to any other monad. For example, the monad returned by the state
function is actually stateT
applied to the identity monad.
The examples apply promiseT to a Javascript Array (which is not a Monad, but Array.prototype.map is a Functor, so we can work with that) and a simple Identity monad, yielding new promise-aware types in each case. These new types are not promises. They don't have then
, but they can operate on promise values and their functor, applicative, and chain accept functions that deal with promises.
Both the Identity monad and it's promiseT
-ified version obey the functor, applicative, chain, and monad laws, as they are stated in fantasy-land.
promiseT
is independent of any particular promise implementation. It relies only on the Promises/A+ then
API. The examples here use when.js, but my original gist version used avow. Switching required no changes to promiseT
There is currently a gap between the Promise and Monad communities, and I've been thinking about ways to bridge that. The two concepts are powerful and elegant, and I think it would be awesome for them to work together, and for the communities to converge.
- Fork/clone
npm install
npm test
- runs functor, applicative, chain, and monad law tests against the Id andpromiseT(Id)
monadsnode examples/list.js
- lifts JavascriptArray
to be promise-aware and does some gymnastics with it.node examples/errors.js
- shows how error handling might work in a way that is similar to Haskell'sEither
/catchError
/throwError
error handling approach.
Copyright © 2013 Brian Cavalier, MIT License