Type of the encapsulated success value.
Whether this ITry represents a successful or failed operation.
Similar to {@link #orTry}, but works like a thenable
-object.
The handler is allowed to return either the new value directly,
or a ITry with the new value.
TryFactory.error("bad").catch(e => 0)
TryFactory.error("bad").catch(e => Try.success(0))
The new value, wrapped in a ITry for encapsulating errors.
Computes a new value by using either the success handler, or the backup handler. If the mapper throws, the backup is applied. If the backup throws as well, the returned ITry is not successful. If the backup handler is not given and this try is unsuccessful, return a try with the same error.
TryFactor.success(9).convert(x => x * 2);
// => 18
TryFactor.error("bad").convert(x => x * x, e => e.message.length);
// => 3
Type of the new value.
Handler that maps the successful value to a new value.
Handler that maps the error to a new value.
The new value, wrapped in a ITry for encapsulating errors.
Computes a new value by using either the success handler, or the backup handler. If the mapper throws, the backup is applied. If the backup throws as well, the returned Try is not successful. If the backup handler is not given and this try is unsuccessful, return a try with the same error. Similar to #convert, but the handlers return a Try directly.
TryFactor.success("9").convert(x => Try.of(() => JSON.parse(x)));
// => Try[value=9]
Type of the new value.
Handler that maps the error to a new value.
The new value, wrapped in a ITry for encapsulating errors.
Calls the error handler iff this try is not successful, or does nothing otherwise.
TryFactory.success(9).ifAbsent(console.log)
// prints nothing
TryFactory.error(new Error).ifAbsent(console.info)
// Informs about error
This ITry..
Calls the success handler with the successful value, or the error handler with the error.
TryFactory.success(9).ifPresent(console.info, console.warn)
// informs about 9
TryFactory.error(new Error).ifPresent(console.info, console.warn)
// warns about error
This ITry.
Creates an iterable over either the successful value or the error.
A stream over either the successful value or the error.
Returns the successful value or the backup value in case this try represents an error.
TryFactory.error("bad").erElse(0) // => 0
The default value in case of an errror.
The successful value; or the default value.
If not successful, attempts to compute a value from the error with the given backup handler. Similar to orTry, but the backup handler returns a ITry directly.
parseIntSafe = string => {
if (isInt(string)) return parseInt(string);
throw new Error("invalid number format")
}
T.of(() => parseIntSafe(input))
.orTryFlat(e => Try.of(() => parseIntSafe(e.statusCode)))
// => input, status code, or error
Backup handler that computes a new value for the error.
A ITry with the current successful value, the new computed backup value, or an error in case the backup handler threw an error.
On success, returns the value; otherwise throws the error.
TryFactory.success(9).orThrow()
// => 9
TryFactory.error(new Error()).orThrow()
// => throws
The successful value.
If not successful, attempts to compute a value from the error with the given backup handler.
parseIntSafe = string => {
if (isInt(string)) return parseInt(string);
throw new Error("invalid number format")
}
T.of(() => parseIntSafe(input))
.orTry(e => parseIntSafe(e.statusCode))
// => input, status code, or error
Backup handler that computes a new value for the error.
A ITry with the current successful value, the new computed backup value, or an error in case the backup handler threw an error.
Creates an stream from either the successful value or the error.
TryFactory.of(() => parseIntSafe("2.5")).stream().filter(x => x instanceof Error).first()
// Error("invalid integer format")
A stream of either the successful value or the error.
Similar to [[Try.convert]], but works like a thenable
-object.
The handlers are allowed to return either the new value directly,
or a ITry with the new value.
TryFactory.of(...).then(x => JSON.parse(x))
TryFactory.of(...).then(x => Try.of(() => JSON.parse(x)))
Type of the new value.
Handler that maps the successful value to a new value.
The new value, wrapped in a ITry for encapsulating errors.
Hook for JSON.stringify. Returns a JSON representation of itself, containing its value and whether it represents a success or failure of an operation.
An object with keys result
(contained value) and success
(whether the operation was successful).
Unwraps the try an returns either the contained successful value or the error.
Either the contained value or the error.
Generated using TypeDoc
A try represents the result of an operation, that may have succeeded or failed, and contains several methods for handling success and error cases.
const t = T.of(() => JSON.parse(input)) // => Try[success=true/false] t.orElse({}); // => The parsed JSON object or an empty object.
Several methods such as {@link ITry.map} take an abitrary callback for manipulating the success or error value. If these methods throw an error, the returned ITry is not successful and contains an error with the stacktrace of the original error added.
const t = T.of(() => JSON.parse(input)) t.mapError(() => {throw new Error("Bad handler")})