Implements IEquals.equals with overridable equality for A
.
Inverts this Try
. If this is a Failure, returns its exception wrapped
in a Success. If this is a Success
, returns a Failure
containing a
NoSuchElementError.
Returns the given function applied to the value if this is
a Success or returns this
if this is a Failure.
This operation is the monadic "bind" operation.
It can be used to chain multiple Try
references.
Try.of(() => parse(s1)).flatMap(num1 =>
Try.of(() => parse(s2)).map(num2 =>
num1 / num2
))
Applies the failure
function to Failure values, and the
success
function to Success values and returns the result.
const maybeNum: Try<number> =
tryParseInt("not a number")
const result: string =
maybeNum.fold(
error => `Could not parse string: ${error}`,
num => `Success: ${num}`
)
Returns the value from a Success
or the given fallback
value if this is a Failure
.
Success(10).getOrElse(27) // 10
Failure("error").getOrElse(27) // 27
Returns the value from a Success
or the value generated
by a given thunk
in case this is a Failure
.
Success(10).getOrElseL(() => 27) // 10
Failure("error").getOrElseL(() => 27) // 27
Returns a Try
containing the result of applying f
to
this option's value, but only if it's a Success
, or
returns the current Failure
without any modifications.
NOTE: this is similar with flatMap
, except with map
the
result of f
doesn't need to be wrapped in a Try
.
the mapping function that will transform the value
of this Try
if successful.
a new Try
instance containing the value of the
source mapped by the given function
Returns the current value if it's a Success, or if the source
is a Failure then return the value generated by the given
thunk
.
Success(10).orElseL(() => Success(17)) // 10
Failure("error").orElseL(() => Success(17)) // 17
Success(10).orNull() // 10
Failure("error").orNull() // null
This can be useful for use-cases such as:
Try.of(() => dict.user.profile.name).orNull()
Success(10).orUndefined() // 10
Failure("error").orUndefined() // undefined
This can be useful for use-cases such as:
Try.of(() => dict.user.profile.name).orUndefined()
Applies the given function f
if this is a Failure
, otherwise
returns this
if this is a Success
.
This is like map
for the exception.
In the following example, if the user.profile.email
exists,
then it is returned as a successful value, otherwise
Try.of(() => user.profile.email).recover(e => {
// Access error? Default to empty.
if (e instanceof TypeError) return ""
throw e // We don't know what it is, rethrow
})
Note that on rethrow, the error is being caught in `recover` and
it still returns it as a `Failure(e)`.
Applies the given function f
if this is a Failure
, otherwise
returns this
if this is a Success
.
This is like map
for the exception.
In the following example, if the user.profile.email
exists,
then it is returned as a successful value, otherwise
Try.of(() => user.profile.email).recover(e => {
// Access error? Default to empty.
if (e instanceof TypeError) return ""
throw e // We don't know what it is, rethrow
})
Note that on rethrow, the error is being caught in `recover` and
it still returns it as a `Failure(e)`.
Transforms the source into an Either.
In case the source is a Success(v)
, then it gets translated
into a Right(v)
. If the source is a Failure(e)
, then a Left(e)
value is returned.
Success("value").toEither() // Right("value")
Failure("error").toEither() // Left("error")
Maps 2 Try
values by the mapping function, returning a new
Try
reference that is a Success
only if both Try
values are
a Success
, otherwise it returns the first Failure
noticed.
// Yields Success(3)
Try.map2(Success(1), Success(2),
(a, b) => a + b
)
// Yields Failure, because the second arg is a Failure
Try.map2(Success(1), Failure("error"),
(a, b) => a + b
)
This operation is the Applicative.map2
.
Maps 3 Try
values by the mapping function, returning a new
Try
reference that is a Success
only if all 3 Try
values are
a Success
, otherwise it returns the first Failure
noticed.
// Yields Success(6)
Try.map3(Success(1), Success(2), Success(3),
(a, b, c) => {
return a + b + c
}
)
// Yields Failure, because the second arg is a Failure
Try.map3(
Success(1),
Failure("error"),
Success(3),
(a, b, c) => {
return a + b + c
}
)
Maps 4 Try
values by the mapping function, returning a new
Try
reference that is a Success
only if all 4 Try
values are
a Success
, otherwise it returns the first Failure
noticed.
// Yields Success(10)
Try.map4(Success(1), Success(2), Success(3), Success(4),
(a, b, c, d) => {
return a + b + c + d
}
)
// Yields Failure, because the second arg is a Failure
Try.map3(
Success(1),
Failure("error"),
Success(3),
Success(4),
(a, b, c, d) => {
return a + b + c + d
}
)
Maps 5 Try
values by the mapping function, returning a new
Try
reference that is a Success
only if all 5 Try
values are
a Success
, otherwise it returns the first Failure
noticed.
// Yields Success(15)
Try.map5(
Success(1),
Success(2),
Success(3),
Success(4),
Success(5),
(a, b, c, d, e) => {
return a + b + c + d + e
}
)
// Yields Failure, because the second arg is a Failure
Try.map5(
Success(1),
Failure("error"),
Success(3),
Success(4),
Success(5),
(a, b, c, d, e) => {
return a + b + c + d + e
}
)
Maps 6 Try
values by the mapping function, returning a new
Try
reference that is a Success
only if all 6 Try
values are
a Success
, otherwise it returns the first Failure
noticed.
// Yields Success(21)
Try.map6(
Success(1),
Success(2),
Success(3),
Success(4),
Success(5),
Success(6),
(a, b, c, d, e, f) => {
return a + b + c + d + e + f
}
)
// Yields Failure, because the second arg is a Failure
Try.map6(
Success(1),
Failure("error"),
Success(3),
Success(4),
Success(5),
Success(6),
(a, b, c, d, e, f) => {
return a + b + c + d + e + f
}
)
Alias of Try.success.
Alias for Try.failure and Failure,
wrapping any throwable into a Try
value.
Keeps calling f
until a Right(b)
is returned.
Based on Phil Freeman's Stack Safety for Free.
Described in FlatMap.tailRecM
.
Shorthand for now(undefined as void)
, always returning
the same reference as optimization.
Generated using TypeDoc
The
Success
data constructor is for building Try values that are successful results of computations, as opposed to Failure.