Skip to content

Commit

Permalink
feat(result): Add onValue and onError handlers (#24099)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Aug 27, 2023
1 parent a1c1798 commit b2c6cbe
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
56 changes: 56 additions & 0 deletions lib/util/result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,34 @@ describe('util/result', () => {
expect(Result.err('oops').parse(schema)).toEqual(Result.err('oops'));
});
});

describe('Handlers', () => {
it('supports value handlers', () => {
const cb = jest.fn();
Result.ok(42).onValue(cb);
expect(cb).toHaveBeenCalledWith(42);
});

it('supports error handlers', () => {
const cb = jest.fn();
Result.err('oops').onError(cb);
expect(cb).toHaveBeenCalledWith('oops');
});

it('handles error thrown in value handler', () => {
const res = Result.ok(42).onValue(() => {
throw 'oops';
});
expect(res).toEqual(Result._uncaught('oops'));
});

it('handles error thrown in error handler', () => {
const res = Result.err('oops').onError(() => {
throw 'oops';
});
expect(res).toEqual(Result._uncaught('oops'));
});
});
});

describe('AsyncResult', () => {
Expand Down Expand Up @@ -621,4 +649,32 @@ describe('util/result', () => {
expect(res).toEqual(Result._uncaught('oops'));
});
});

describe('Handlers', () => {
it('supports value handlers', async () => {
const cb = jest.fn();
await AsyncResult.ok(42).onValue(cb);
expect(cb).toHaveBeenCalledWith(42);
});

it('supports error handlers', async () => {
const cb = jest.fn();
await AsyncResult.err('oops').onError(cb);
expect(cb).toHaveBeenCalledWith('oops');
});

it('handles error thrown in value handler', async () => {
const res = await AsyncResult.ok(42).onValue(() => {
throw 'oops';
});
expect(res).toEqual(Result._uncaught('oops'));
});

it('handles error thrown in error handler', async () => {
const res = await AsyncResult.err('oops').onError(() => {
throw 'oops';
});
expect(res).toEqual(Result._uncaught('oops'));
});
});
});
52 changes: 52 additions & 0 deletions lib/util/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,36 @@ export class Result<T extends Val, E extends Val = Error> {

return Result.err(err);
}

/**
* Call `fn` on the `val` if the result is ok.
*/
onValue(fn: (value: T) => void): Result<T, E> {
if (this.res.ok) {
try {
fn(this.res.val);
} catch (err) {
return Result._uncaught(err);
}
}

return this;
}

/**
* Call `fn` on the `err` if the result is err.
*/
onError(fn: (err: E) => void): Result<T, E> {
if (!this.res.ok) {
try {
fn(this.res.err);
} catch (err) {
return Result._uncaught(err);
}
}

return this;
}
}

/**
Expand Down Expand Up @@ -828,4 +858,26 @@ export class AsyncResult<T extends Val, E extends Val>
)
);
}

onValue(fn: (value: T) => void): AsyncResult<T, E> {
return new AsyncResult(
this.asyncResult
.then((result) => result.onValue(fn))
.catch(
/* istanbul ignore next: should never happen */
(err) => Result._uncaught(err)
)
);
}

onError(fn: (err: E) => void): AsyncResult<T, E> {
return new AsyncResult(
this.asyncResult
.then((result) => result.onError(fn))
.catch(
/* istanbul ignore next: should never happen */
(err) => Result._uncaught(err)
)
);
}
}

0 comments on commit b2c6cbe

Please sign in to comment.