From 5d3b1c4f9cfe13f55418442e220d3fd65cd72ba7 Mon Sep 17 00:00:00 2001 From: henry cunha Date: Sun, 26 Feb 2023 10:23:52 -0300 Subject: [PATCH] feat: add a method for returning an object This will allow for handling errors or data using a single reference --- README.md | 18 ++++++++++++++++++ src/index.test.ts | 47 ++++++++++++++++++++++++++++++++++++++++++++++- src/index.ts | 13 +++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 59f825b..f594202 100644 --- a/README.md +++ b/README.md @@ -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? diff --git a/src/index.test.ts b/src/index.test.ts index 708d904..9132db0 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -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 () => { @@ -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", + }); + }); + }); +}); diff --git a/src/index.ts b/src/index.ts index 356d112..13c5b3c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,3 +12,16 @@ export const trytm = async ( throw throwable; } }; + +export const try$ = async ( + promise: Promise, +): 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; + } +};