Skip to content

feat: add a method for returning an object #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,25 @@ async function awesome() {

// ...
}
```

You can also use the `try$` function to collapse the error and data into a single object:

```TS
import { try$ } from "@bdsqqq/try"

async function awesome() {
const a = await try$(step1());
if(a.error) // ...

const b = await try$(step2(a.data));
if(b.error) // ...

const c = await try$(step3(b.data));
if(c.error) // ...

// ...
}
```

### Why does this REALLY exist?
Expand Down
47 changes: 46 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, vi, expect } from "vitest";
import { trytm } from ".";
import { trytm, try$ } from ".";

describe("tryTm", () => {
it("Should return promise value if the promise resolves", async () => {
Expand Down Expand Up @@ -45,3 +45,48 @@ describe("tryTm", () => {
});
});
});

describe("try$", () => {
it("Should return promise value if the promise resolves", async () => {
const promiseFn = vi
.fn()
.mockImplementationOnce(async () =>
Promise.resolve({ hey: "Bedesqui" }),
);

const { data, error } = await try$(promiseFn());

expect(data).toStrictEqual({ hey: "Bedesqui" });
expect(error).toBeNull();
});

it("Should return error if the promise rejects with an Error value", async () => {
const promiseFn = vi
.fn()
.mockImplementationOnce(async () =>
Promise.reject(new Error("I'm a failure")),
);

const { data, error } = await try$(promiseFn());

expect(data).toBeNull();
expect(error).toBeInstanceOf(Error);
expect((error as Error).message).toBe("I'm a failure");
});

it("Should throw if the promise rejects with an non-Error value", async () => {
expect.assertions(1);

const promiseFn = vi
.fn()
.mockImplementationOnce(async () =>
Promise.reject({ someNonErrorValue: "Maybe I'm not a failure" }),
);

await try$(promiseFn()).catch((throwable) => {
expect(throwable).toEqual({
someNonErrorValue: "Maybe I'm not a failure",
});
});
});
});
13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,16 @@ export const trytm = async <T>(
throw throwable;
}
};

export const try$ = async <T>(
promise: Promise<T>,
): Promise<{ data: T | null; error: Error | null }> => {
try {
const data = await promise;
return { data, error: null };
} catch (throwable) {
if (throwable instanceof Error) return { data: null, error: throwable };

throw throwable;
}
};