@ikasoba000/result-ts
v1.0.1
Published
<p align="center"> result-ts provides a class that is useful when dealing with exceptions in TypeScript. </p>
Downloads
4
Readme
import { Result } from "@ikasoba000/result-ts";
async function fetchUser(): Promise<Result<User, number>> {
const response = await fetch( ... );
if (!response.ok) return Result.err(response.status);
return Result.ok(await response.json());
}
const result = await fetchUser()
if (result.isError()){
console.error(
"Failed to fetch user."
+ `Status code: ${result.value}`
);
process.exit(1);
}
console.log(`hello, ${result.value.name}!`)
Result.ok<T>(value: T): Result<T, never>
Returns a successful
Result.
Result.err<E>(value: E): Result<never, E>
Returns a Result
containing an exception.
Result.toCurrentResult<T, E>(result: ResultLike<T, E>): Result<T, E>
Generates a Result
from a ResultLike
.
This is useful when converting Results between different versions.
Result#isOk(): boolean
Check to see if the Result was successful.
Result#isError(): boolean
Check to see if the Result failed.