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)`.
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
Try
type represents a computation that may either result in an exception, or return a successfully computed value. It's similar to, but semantically different from the Either type.Try
is a sum type and so instances ofTry
are either instances of Success or of Failure.For example,
Try
can be used to perform division on a user-defined input, without the need to do explicit exception-handling in all of the places that an exception might occur.Example:
function divide(dividendS: string, divisorS: string): string { const dividend = Try(() => parseInt(dividendS)) .filter(_ => _ === _) // filter out NaN const divisor = Try(() => parseInt(divisorS)) .filter(_ => _ === _) // filter out NaN // map2 executes the given function only if both results are // successful; we could also express this with flatMap / chain const result = Try.map2(dividend, divisor, (a, b) => a / b ) result.fold( error => `failure: ${error}` result => `result: ${result}` ) }
An important property of
Try
is its ability to pipeline, or chain, operations, catching exceptions along the way. TheflatMap
andmap
combinators each essentially pass off either their successfully completed value, wrapped in the Success type for it to be further operated upon by the next combinator in the chain, or the exception wrapped in the Failure type usually to be simply passed on down the chain. Combinators such asrecover
andrecoverWith
are designed to provide some type of global behavior in the case of failure.NOTE: all
Try
combinators will catch exceptions and return failure unless otherwise specified in the documentation.