Result
A Result represents a successful value or an error. It forces the consumer to check whether the returned type is an error or not, result.ok
acts as a discriminant between success and failure.
For example, when processing the return type for the chat function you can do the following
const chatResult = chat(chatOptions);
if (!chatResult.ok) {
handleError(chatResult.error);
return;
}
console.log(chatResult.value.message)
The library never throws when there's an error, instead it returns an error result, and the value of the result (i.e. result.error
) will be a request error.
Result type
Signature:
type Result<T, E> = OkResult<T> | ErrResult<E>;
References: OkResult, ErrResult
OkResult
Represents a successful result where you can access value
Signature:
interface OkResult<T>
Properties
Property | Type | Description |
---|---|---|
ok | true | |
value | T |
ErrResult
Represents a failure result
Signature:
interface ErrResult<E>
Properties
Property | Type | Description |
---|---|---|
error | E | Usually a request error |
ok | false |