The function to apply the value to if result
is Ok
.
The Result
instance to map over.
A new Result
with the result of applying mapFn
to the value
in an Ok
, or else the original Err
value wrapped in the new
instance.
T The type of the value wrapped in an Ok
instance, and taken as
the argument to the mapFn
.
U The type of the value wrapped in the new Ok
instance after
applying mapFn
, that is, the type returned by mapFn
.
E The type of the value wrapped in an Err
instance.
Generated using TypeDoc
Map over a
Result
instance: apply the function to the wrapped value if the instance isOk
, and return the wrapped error value wrapped as a newErr
of the correct type (Result<U, E>
) if the instance isErr
.map
works a lot likeArray.prototype.map
, but with one important difference. BothResult
andArray
are containers for other kinds of items, but whereArray.prototype.map
has 0 to n items, aResult
always has exactly one item, which is either a success or an error instance.Where
Array.prototype.map
will apply the mapping function to every item in the array (if there are any),Result.map
will only apply the mapping function to the (single) element if anOk
instance, if there is one.If you have no items in an array of numbers named
foo
and callfoo.map(x => x + 1)
, you'll still some have an array with nothing in it. But if you have any items in the array ([2, 3]
), and you callfoo.map(x => x + 1)
on it, you'll get a new array with each of those items inside the array "container" transformed ([3, 4]
).With this
map
, theErr
variant is treated by themap
function kind of the same way as the empty array case: it's just ignored, and you get back a newResult
that is still just the sameErr
instance. But if you have anOk
variant, the map function is applied to it, and you get back a newResult
with the value transformed, and still wrapped in anOk
.Examples