Skip to content

Commit 7fdfcad

Browse files
committed
fixes issue where AsyncResult onSuccess and onFailure did not handle promises well
1 parent c6d38a9 commit 7fdfcad

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": false,
33
"name": "typescript-result",
4-
"version": "2.1.0",
4+
"version": "2.1.1",
55
"description": "A Result type inspired by Rust and Kotlin that leverages TypeScript's powerful type system to simplify error handling and make your code more readable and maintainable.",
66
"keywords": [
77
"result",

src/result.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,16 @@ describe("AsyncResult", () => {
18861886
expect(spy).toHaveBeenCalled();
18871887
});
18881888

1889+
it("handles callbacks that return a promise as well", async () => {
1890+
const result = AsyncResult.error(errorA);
1891+
1892+
const spy = vi.fn();
1893+
1894+
await result.onFailure(() => Promise.resolve().then(sleep).then(spy));
1895+
1896+
expect(spy).toHaveBeenCalled();
1897+
});
1898+
18891899
it("throws an error when the callback throws an error", async () => {
18901900
await expect(() =>
18911901
AsyncResult.error(errorA).onFailure(() => {
@@ -1944,6 +1954,16 @@ describe("AsyncResult", () => {
19441954
}),
19451955
).rejects.toThrow(CustomError);
19461956
});
1957+
1958+
it("handles callbacks that return a promise as well", async () => {
1959+
const result = AsyncResult.ok(12);
1960+
1961+
const spy = vi.fn();
1962+
1963+
await result.onSuccess(() => Promise.resolve().then(sleep).then(spy));
1964+
1965+
expect(spy).toHaveBeenCalled();
1966+
});
19471967
});
19481968

19491969
describe("map", () => {

src/result.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,15 @@ export class AsyncResult<Value, Err> extends Promise<Result<Value, Err>> {
212212
* .map((value) => value * 2); // proceed with other operations
213213
* ```
214214
*/
215-
onFailure(action: (error: Err) => void): AsyncResult<Value, Err> {
215+
onFailure(
216+
action: (error: Err) => void | Promise<void>,
217+
): AsyncResult<Value, Err> {
216218
return new AsyncResult<Value, Err>((resolve, reject) =>
217219
this.then(async (result) => {
218220
try {
219-
await result.onFailure(action);
221+
if (result.isError()) {
222+
await action(result.error as Err);
223+
}
220224
resolve(result);
221225
} catch (e) {
222226
reject(e);
@@ -254,11 +258,15 @@ export class AsyncResult<Value, Err> extends Promise<Result<Value, Err>> {
254258
* const asyncResult = await result.onSuccess(async (value) => someAsyncOperation(value));
255259
* ```
256260
*/
257-
onSuccess(action: (value: Value) => void): AsyncResult<Value, Err> {
261+
onSuccess(
262+
action: (value: Value) => void | Promise<void>,
263+
): AsyncResult<Value, Err> {
258264
return new AsyncResult<Value, Err>((resolve, reject) =>
259265
this.then(async (result) => {
260266
try {
261-
await result.onSuccess(action);
267+
if (result.isOk()) {
268+
await action(result.value as Value);
269+
}
262270
resolve(result);
263271
} catch (error) {
264272
reject(error);

0 commit comments

Comments
 (0)