From a4e7e421dd90edc372ee5a4de06e8909580edb74 Mon Sep 17 00:00:00 2001 From: sharno Date: Fri, 16 Aug 2024 16:51:24 -0400 Subject: [PATCH 1/6] Return ResultAsync from safeTry instead of Promise> for better composability --- src/result.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/result.ts b/src/result.ts index d046f4e3..f129a5f7 100644 --- a/src/result.ts +++ b/src/result.ts @@ -104,26 +104,24 @@ export function safeTry< // Promise>, not ResultAsync, is used as the return type. export function safeTry( body: () => AsyncGenerator, Result>, -): Promise> +): ResultAsync export function safeTry< YieldErr extends Err, GeneratorReturnResult extends Result >( body: () => AsyncGenerator, -): Promise< - Result< - InferOkTypes, - InferErrTypes | InferErrTypes - > +): ResultAsync< + InferOkTypes, + InferErrTypes | InferErrTypes > export function safeTry( body: | (() => Generator, Result>) | (() => AsyncGenerator, Result>), -): Result | Promise> { +): Result | ResultAsync { const n = body().next() if (n instanceof Promise) { - return n.then((r) => r.value) + return new ResultAsync(n.then((r) => r.value)) } return n.value } From 547352f326206b2c5b403bde4ddc88825172f25c Mon Sep 17 00:00:00 2001 From: sharno Date: Fri, 16 Aug 2024 17:23:53 -0400 Subject: [PATCH 2/6] add changeset --- .changeset/safeTryAsyncResult.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/safeTryAsyncResult.md diff --git a/.changeset/safeTryAsyncResult.md b/.changeset/safeTryAsyncResult.md new file mode 100644 index 00000000..e8ebbc80 --- /dev/null +++ b/.changeset/safeTryAsyncResult.md @@ -0,0 +1,5 @@ +--- +'neverthrow': patch +--- + +change the return type of `safeTry` to be `ResultAsync` instead of `Promise>` for better composability From 6b99d2f2823ec5b7a238196818eb36ceaff9b127 Mon Sep 17 00:00:00 2001 From: sharno Date: Sun, 18 Aug 2024 00:43:32 -0400 Subject: [PATCH 3/6] changeset to minor --- .changeset/safeTryAsyncResult.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/safeTryAsyncResult.md b/.changeset/safeTryAsyncResult.md index e8ebbc80..fe7ed3c7 100644 --- a/.changeset/safeTryAsyncResult.md +++ b/.changeset/safeTryAsyncResult.md @@ -1,5 +1,5 @@ --- -'neverthrow': patch +'neverthrow': minor --- change the return type of `safeTry` to be `ResultAsync` instead of `Promise>` for better composability From 6be2235fd740cfc694c05067a2e17ef8e33195af Mon Sep 17 00:00:00 2001 From: sharno Date: Thu, 22 Aug 2024 00:14:50 -0400 Subject: [PATCH 4/6] fix type tests --- tests/typecheck-tests.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/typecheck-tests.ts b/tests/typecheck-tests.ts index c637d5ca..bdd6058b 100644 --- a/tests/typecheck-tests.ts +++ b/tests/typecheck-tests.ts @@ -2091,7 +2091,7 @@ type CreateTuple = name: 'ReturnMyError' } - type Expectation = Promise> + type Expectation = ResultAsync const result = safeTry(async function *() { return okAsync('string'); @@ -2106,7 +2106,7 @@ type CreateTuple = name: 'ReturnMyError'; } - type Expectation = Promise> + type Expectation = ResultAsync const result = safeTry(async function *() { return errAsync({ name: 'ReturnMyError' }); @@ -2147,7 +2147,7 @@ type CreateTuple = name: 'ReturnMyError'; } - type Expectation = Promise> + type Expectation = ResultAsync const result = safeTry(async function *() { yield* okAsync(123).safeUnwrap(); From aabb021341d2e2cbe62d6efaf00de205fcb9f390 Mon Sep 17 00:00:00 2001 From: sharno Date: Thu, 22 Aug 2024 00:22:01 -0400 Subject: [PATCH 5/6] remove comment --- src/result.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/result.ts b/src/result.ts index 1a0a9882..f04f53af 100644 --- a/src/result.ts +++ b/src/result.ts @@ -103,9 +103,6 @@ export function safeTry< * `yield* resultAsync.safeUnwrap()` work as Rust's `result?` expression. * @returns The first occurence of either an yielded Err or a returned Result. */ -// NOTE: -// Since body is potentially throwable because `await` can be used in it, -// Promise>, not ResultAsync, is used as the return type. export function safeTry( body: () => AsyncGenerator, Result>, ): ResultAsync From c86f88e0058e314f1b72cacb0bae9b0415f3a726 Mon Sep 17 00:00:00 2001 From: sharno Date: Thu, 22 Aug 2024 00:25:27 -0400 Subject: [PATCH 6/6] fix more tests --- tests/safe-try.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/safe-try.test.ts b/tests/safe-try.test.ts index 3983ae9f..b87308ff 100644 --- a/tests/safe-try.test.ts +++ b/tests/safe-try.test.ts @@ -177,7 +177,7 @@ describe("Tests if README's examples work", () => { }) test("async mayFail1 error", async () => { - function myFunc(): Promise> { + function myFunc(): ResultAsync { return safeTry(async function*() { return ok( (yield* (await promiseBad()) @@ -197,7 +197,7 @@ describe("Tests if README's examples work", () => { }) test("async mayFail2 error", async () => { - function myFunc(): Promise> { + function myFunc(): ResultAsync { return safeTry(async function*() { return ok( (yield* (await promiseGood()) @@ -217,7 +217,7 @@ describe("Tests if README's examples work", () => { }) test("promise async all ok", async () => { - function myFunc(): Promise> { + function myFunc(): ResultAsync { return safeTry(async function*() { return ok( (yield* (await promiseGood())