yunlonggong's picture
Initial project upload
1b44660
raw
history blame contribute delete
595 Bytes
import { type Result, err, ok } from 'neverthrow';
/**
* Wraps an existing Promise, converting resolution to Ok and rejection/throw to Err.
* The error type is 'unknown' because anything can be thrown.
*
* @param promise The promise to wrap.
* @returns A Promise resolving to a Result<T, unknown>.
*/
export async function tryCatchAsync<T>(promise: Promise<T>): Promise<Result<T, unknown>> {
try {
const value = await promise;
return ok(value);
} catch (error) {
// Catches synchronous throws during promise creation *and* promise rejections.
return err(error);
}
}