An alternative effect monad for PureScript.
Use this for easy interop with existing promise-based JavaScript libraries.
With monadic do
notation, or with promise-chaining notation. The following are
equivalent:
myApply :: forall r a b. Promise r (a -> b) -> Promise r a -> Promise r b
myApply pab pa = do
ab <- pab
a <- pa
pure (ab a)
myApplyChained :: forall r a b. Promise r (a -> b) -> Promise r a -> Promise r b
myApplyChained pab pa = pab # then' \ ab -> pa # then' \ a -> resolve (ab a)
in fact, if you squint a little, myApplyChained
looks like the following JavaScript:
var myApplyChained = function (pab, pa) {
pab.then(function (ab) {
pa.then(function (a) {
return Promise.resolve(ab(a));
});
});
}
Also see the tests
folder.
Note: While promises are eager,
this library provides the Deferred
typeclass to ensure promises don't prematurely
run their side-effects until safely consumed with runPromise
, or the nonstandard
done
.
exports.myPromise = new Promise(function (resolve, reject) {
resolve(5);
});
foreign import myPromise :: forall r. Promise r Int
doSomething :: Deferred => Promise (console :: CONSOLE | r) Unit
doSomething = do
p <- myPromise
Console.logShow p
main :: forall r. Eff (console :: CONSOLE | r) Unit
main
= runPromise
(const (log "success callback"))
(const (error "error callback"))
doSomething
bower install --save purescript-promises