mirror of
https://github.com/renovatebot/renovate.git
synced 2025-01-10 22:16:28 +00:00
feat: Wrap callback returning Promise for Result
utility (#32788)
This commit is contained in:
parent
b01dcc78e4
commit
baea715bea
2 changed files with 22 additions and 2 deletions
|
@ -31,18 +31,29 @@ describe('util/result', () => {
|
|||
});
|
||||
|
||||
describe('Wrapping', () => {
|
||||
it('wraps callback', () => {
|
||||
it('wraps callback returning value', () => {
|
||||
const res = Result.wrap(() => 42);
|
||||
expect(res).toEqual(Result.ok(42));
|
||||
});
|
||||
|
||||
it('handles callback error', () => {
|
||||
it('handles throw in callback', () => {
|
||||
const res = Result.wrap(() => {
|
||||
throw 'oops';
|
||||
});
|
||||
expect(res).toEqual(Result.err('oops'));
|
||||
});
|
||||
|
||||
it('wraps callback returning promise', () => {
|
||||
const res = Result.wrap(() => Promise.resolve(42));
|
||||
expect(res).toEqual(AsyncResult.ok(42));
|
||||
});
|
||||
|
||||
it('wraps callback returning failed promise', () => {
|
||||
const err = new Error('unknown');
|
||||
const res = Result.wrap(() => Promise.reject(err));
|
||||
expect(res).toEqual(AsyncResult.err(err));
|
||||
});
|
||||
|
||||
it('wraps nullable callback', () => {
|
||||
const res: Result<number, 'oops'> = Result.wrapNullable(
|
||||
(): number | null => 42,
|
||||
|
|
|
@ -142,6 +142,9 @@ export class Result<T extends Val, E extends Val = Error> {
|
|||
static wrap<T extends Val, E extends Val = Error>(
|
||||
callback: () => RawValue<T>,
|
||||
): Result<T, E>;
|
||||
static wrap<T extends Val, E extends Val = Error>(
|
||||
callback: () => Promise<RawValue<T>>,
|
||||
): AsyncResult<T, E>;
|
||||
static wrap<T extends Val, E extends Val = Error, EE extends Val = never>(
|
||||
promise: Promise<Result<T, EE>>,
|
||||
): AsyncResult<T, E | EE>;
|
||||
|
@ -157,6 +160,7 @@ export class Result<T extends Val, E extends Val = Error> {
|
|||
input:
|
||||
| SafeParseReturnType<Input, T>
|
||||
| (() => RawValue<T>)
|
||||
| (() => Promise<RawValue<T>>)
|
||||
| Promise<Result<T, EE>>
|
||||
| Promise<RawValue<T>>,
|
||||
): Result<T, ZodError<Input>> | Result<T, E | EE> | AsyncResult<T, E | EE> {
|
||||
|
@ -170,6 +174,11 @@ export class Result<T extends Val, E extends Val = Error> {
|
|||
|
||||
try {
|
||||
const result = input();
|
||||
|
||||
if (result instanceof Promise) {
|
||||
return AsyncResult.wrap(result);
|
||||
}
|
||||
|
||||
return Result.ok(result);
|
||||
} catch (error) {
|
||||
return Result.err(error);
|
||||
|
|
Loading…
Reference in a new issue