Overview
Maps a ResultAsync<T, E> to ResultAsync<T, F> by applying a function to the contained Err value. The Ok value is left untouched.
Signature
class ResultAsync<T, E> {
mapErr<U>(f: (e: E) => U | Promise<U>): ResultAsync<T, U>
}
f
(e: E) => U | Promise<U>
required
Function to transform the Err value. Can return either a value or a Promise.
Returns: ResultAsync<T, U> - A new ResultAsync with the transformed error
Usage
Convert error types
type ApiError = { code: number, message: string }
const result = await fetchData()
.mapErr((error: Error) => ({
code: 500,
message: error.message
}))
// Result<Data, ApiError>
Async error mapping
const result = await queryDatabase(sql)
.mapErr(async (dbError) => {
// Log error asynchronously
await logError(dbError)
return 'Database query failed'
})
Add context to errors
const processFile = (filename: string) => {
return readFile(filename)
.mapErr(err => `Failed to read ${filename}: ${err}`)
.andThen(parseJSON)
.mapErr(err => `Failed to parse ${filename}: ${err}`)
}
Key characteristics
- Short-circuits on Ok: If the ResultAsync is Ok, mapErr is not executed
- Type transformation: Allows changing the error type in the Result
- Async support: Can handle both sync and async error transformation functions
Use mapErr to normalize error types across different operations, making error handling consistent.
map
Transform the Ok value
orElse
Recover from errors