Repository navigation
Promises
Homam Hosseini edited this page May 12, 2014
·
5 revisions
pure :: x -> Promise xpromise-monad.pure creates a promise from an object that resolves immediately.
fmap :: (x -> y) -> Promise x -> Promise ypromise-monad.fmap maps a normal function over a promise.
bind :: Promise x -> (x -> Promise y) -> Promise ypromise-monad.bind sequentially composes two promises, passing the value produced
by the first as an argument to the second.
Example:
{promises: {promise-monad}} = require \async-ls
get = ->
read = from-error-value-callback fs~read-file
(data1) <- promise-monad.bind read 'foo', 'utf8'
(data2) <- promise-monad.bind read 'bar', 'utf8'
(data3) <- promise-monad.ffmap read 'baz', 'utf8'
[data1, data2, data3]
get!.then do
(list) -> console.log map (.length), list # this will print a list of 3 integers (file sizes for foo, bar and baz).
(error) -> console.log \error, error
`promise-monad` also has `ffmap` and `fbind` functions that are respectively flipped versions of `fmap` and `bind`.Use to-callback function:
(error, list) <- to-callback get!
console.log error, map (.length), list