Ignoring typeclasses, we have the following types in Haskell (we will say having the right Haskell typeclasses corresponds to having a suitable .then
method in JavaScript):
fmap :: (a -> b) -> f a -> f b
bind :: (a -> f b) -> f a -> f b
And in JavaScript we have (made up syntax):
.then :: (this :: f a) -> (a -> (b || f b)) -> f b
So in one sense they are equivalent, but in another not. For example, suppose some kind of promise type called P
in Haskell and we want to read a URL from a file and then give a promise of fetching that URL:
read :: String -> P String
fetch :: String -> P String
readFetch :: String -> P (P String)
readFetch file = fmap fetch (read file)
And then later you might do
:
fetched <- readFetch someFile
...
foo <- fetched
While in JavaScript if you did read(file).then(fetch)
this would be equivalent to the following Haskell:
readFetch :: String -> P String
readFetch file = bind fetch (read file)
So the first only becomes fulfilled once the file is read but the second once the fetch is complete (i.e. later).
We conclude that then
is similar but not exactly the same as fmap
.