mirror of
https://github.com/invoke-ai/InvokeAI.git
synced 2026-02-13 18:25:28 -05:00
feat(ui): loosen typings for Result
This commit is contained in:
@@ -57,7 +57,7 @@ export class Err<E> {
|
||||
* @template T The type of the value in the `Ok` case.
|
||||
* @template E The type of the error in the `Err` case.
|
||||
*/
|
||||
type Result<T, E = Error> = Ok<T> | Err<E>;
|
||||
type Result<T, E> = Ok<T> | Err<E>;
|
||||
|
||||
/**
|
||||
* Creates a successful result.
|
||||
@@ -85,11 +85,12 @@ export function ErrResult<E>(error: E): Err<E> {
|
||||
* @param {() => T} fn The function to execute.
|
||||
* @returns {Result<T>} An `Ok` result if the function succeeds, or an `Err` result if it throws an error.
|
||||
*/
|
||||
export function withResult<T>(fn: () => T): Result<T> {
|
||||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
||||
export function withResult<T>(fn: () => T): Result<T, any> {
|
||||
try {
|
||||
return new Ok(fn());
|
||||
} catch (error) {
|
||||
return new Err(error instanceof Error ? error : new Error(String(error)));
|
||||
return new Err(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,11 +100,12 @@ export function withResult<T>(fn: () => T): Result<T> {
|
||||
* @param {() => Promise<T>} fn The asynchronous function to execute.
|
||||
* @returns {Promise<Result<T>>} A `Promise` resolving to an `Ok` result if the function succeeds, or an `Err` result if it throws an error.
|
||||
*/
|
||||
export async function withResultAsync<T>(fn: () => Promise<T>): Promise<Result<T>> {
|
||||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
||||
export async function withResultAsync<T>(fn: () => Promise<T>): Promise<Result<T, any>> {
|
||||
try {
|
||||
const result = await fn();
|
||||
return new Ok(result);
|
||||
} catch (error) {
|
||||
return new Err(error instanceof Error ? error : new Error(String(error)));
|
||||
return new Err(error);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user