The fromThrowable function wraps a potentially throwing function in a try-catch block and converts it into a function that returns a Result. This is essential when working with third-party libraries or native JavaScript functions that throw exceptions.fromThrowable is a top-level export that references Result.fromThrowable.
import { fromThrowable } from 'neverthrow'// Error type will be 'unknown'const safeJsonParse = fromThrowable(JSON.parse)const result = safeJsonParse('{}')// result type: Result<any, unknown>
Without an error mapper function, the error type will be unknown, which reduces type safety. Always provide an error mapper when possible.
const safeParseConfig = fromThrowable( JSON.parse, () => 'ParseError' as const)// Type system enforces error handlingconst result = safeParseConfig(jsonString)// Must handle both Ok and Err cases