Safely execute operations that might throw
Bridge exception-based code with functional error handling
Safely execute operations that might throw exceptions.
Try wraps operations that might throw exceptions, converting them to Success or Failure values. It bridges exception-based code with functional error handling.
import { Try } from "functype/try"
// Wrap potentially throwing code
const result = Try(() => JSON.parse(jsonString))
// Check state
result.isSuccess() // true if no exception
result.isFailure() // true if exception thrown
// Extract values
result.orElse({}) // value or default
result.orThrow() // value or re-throw
result.toEither() // Either<Error, T>
| Method | Description |
|---|---|
Try(() => value) | Wrap a potentially throwing function |
Try.of(() => value) | Same as constructor |
Try.success(value) | Create a Success directly |
Try.failure(error) | Create a Failure directly (Error or string) |
Try.fromPromise(p) | Create Try from a Promise (async) |
// Map - transform success value
Try(() => "hello").map((s) => s.toUpperCase()) // Success("HELLO")
Try(() => {
throw new Error()
}).map((s) => s.toUpperCase()) // Failure
// FlatMap - chain Try operations
Try(() => readFile(path))
.flatMap((content) => Try(() => JSON.parse(content)))
.flatMap((data) => Try(() => validate(data)))
// Recover - handle failures by mapping over the error
Try(() => riskyOperation()).recover((error) => fallbackValue)
// RecoverWith - handle with another Try
Try(() => primarySource()).recoverWith((error) => Try(() => backupSource()))
// Using fold
const message = Try(() => fetchData()).fold(
(error) => `Failed: ${error.message}`,
(data) => `Got ${data.length} items`,
)
// Using match
result.match({
Success: (value) => console.log("Got:", value),
Failure: (error) => console.error("Error:", error),
})
// Chain multiple operations
const result = Try(() => readConfig())
.flatMap((config) => Try(() => connectDB(config)))
.flatMap((db) => Try(() => db.query("SELECT * FROM users")))
.recover(() => []) // Return empty array on any failure
// Transform errors
Try(() => riskyCall()).mapFailure((err) => new CustomError(err.message))
// Filter with predicate
Try(() => parseInt(input)).filter(
(n) => n > 0,
() => new Error("Must be positive"),
)
import { Do, $ } from "functype/do"
const result = Do(function* () {
const config = yield* $(Try(() => readConfig()))
const db = yield* $(Try(() => connectDB(config)))
const users = yield* $(Try(() => db.query("SELECT * FROM users")))
return users
}) // Try<User[]>
tryValue.toOption() // None for Failure, Some for Success
tryValue.toEither() // Left(error) or Right(value)
tryValue.toPromise() // Rejected or Resolved Promise
See full API documentation at functype API docs