Spaces:
Running
Running
File size: 595 Bytes
1b44660 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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);
}
}
|