Apply a function to the wrapped value if Just and return a new
Just containing the resulting value; or return Nothing if
Nothing.
This differs from map in that thenFn returns another Maybe.
You can use andThen to combine two functions which both create a Maybe
from an unwrapped type.
You may find the .then method on an ES6 Promise helpful for comparison: if
you have a Promise, you can pass its then method a callback which returns
another Promise, and the result will not be a nested promise, but a single
Promise. The difference is that Promise#then unwraps all layers to only
ever return a single Promise value, whereas Maybe.andThen will not unwrap
nested Maybes.
Apply a function to the wrapped value if
Just
and return a newJust
containing the resulting value; or returnNothing
ifNothing
.This differs from
map
in thatthenFn
returns anotherMaybe
. You can useandThen
to combine two functions which both create aMaybe
from an unwrapped type.You may find the
.then
method on an ES6Promise
helpful for comparison: if you have aPromise
, you can pass itsthen
method a callback which returns anotherPromise
, and the result will not be a nested promise, but a singlePromise
. The difference is thatPromise#then
unwraps all layers to only ever return a singlePromise
value, whereasMaybe.andThen
will not unwrap nestedMaybe
s.This is sometimes also known as
bind
, but not aliased as such becausebind
already means something in JavaScript.Example
(This is a somewhat contrived example, but it serves to show the way the function behaves.)
Note that the result is not
Just(Just(13))
, butJust(13)
!