-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from jsr-core/remove-promise
feat!: remove Promise support from tryOr and tryOrElse
- Loading branch information
Showing
4 changed files
with
24 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,17 @@ | ||
import { tryOrElse } from "./try_or_else.ts"; | ||
/** | ||
* Try to execute a function and return the result or a default value. | ||
* Try to execute a function and return the result or a default value if an error occurs. | ||
* | ||
* ```ts | ||
* import { tryOr } from "@core/errorutil/try-or"; | ||
* import { raise } from "@core/errorutil/raise"; | ||
* | ||
* // Sync | ||
* console.log(tryOr(() => 1, 2)); // 1 | ||
* console.log(tryOr(() => raise("err"), 2)); // 2 | ||
* | ||
* // Async | ||
* console.log(await tryOr(() => Promise.resolve(1), 2)); // 1 | ||
* console.log(await tryOr(() => Promise.reject("err"), 2)); // 2 | ||
* console.log(tryOr(() => { throw "err"; }, 2)); // 2 | ||
* ``` | ||
*/ | ||
export function tryOr<T>(fn: () => T, orValue: T): T; | ||
export function tryOr<T>( | ||
fn: () => Promise<T>, | ||
orValue: T | Promise<T>, | ||
): Promise<T>; | ||
export function tryOr<T>( | ||
fn: () => T | Promise<T>, | ||
orValue: T, | ||
): T | Promise<T> { | ||
return tryOrElse(fn, () => orValue); | ||
export function tryOr<T>(fn: () => T, orValue: T): T { | ||
try { | ||
return fn(); | ||
} catch { | ||
return orValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,20 @@ | ||
import { test } from "@cross/test"; | ||
import { assertEquals, assertRejects, assertThrows } from "@std/assert"; | ||
import { assertEquals, assertThrows } from "@std/assert"; | ||
import { raise } from "./raise.ts"; | ||
import { tryOrElse } from "./try_or_else.ts"; | ||
|
||
const err = new Error("error"); | ||
const resolve = Promise.resolve.bind(Promise); | ||
const reject = Promise.reject.bind(Promise); | ||
|
||
await test("tryOrElse (sync)", () => { | ||
type T = number; | ||
assertEquals(tryOrElse((): T => 1, () => 2), 1); | ||
assertEquals(tryOrElse((): T => raise(err), () => 2), 2); | ||
}); | ||
|
||
await test("tryOrElse with Error (sync)", () => { | ||
type T = number; | ||
assertThrows( | ||
() => tryOrElse((): T => raise(err), () => raise(err)), | ||
Error, | ||
"error", | ||
); | ||
await test("tryOrElse executes the first function and return the result", () => { | ||
assertEquals(tryOrElse(() => 1, () => 2), 1); | ||
}); | ||
|
||
await test("tryOrElse (async)", async () => { | ||
type T = Promise<number>; | ||
assertEquals(await tryOrElse((): T => resolve(1), () => 2), 1); | ||
assertEquals(await tryOrElse((): T => resolve(1), () => resolve(2)), 1); | ||
assertEquals(await tryOrElse((): T => reject(err), () => 2), 2); | ||
assertEquals(await tryOrElse((): T => reject(err), () => resolve(2)), 2); | ||
assertEquals(await tryOrElse((): T => raise(err), () => 2), 2); | ||
assertEquals(await tryOrElse((): T => raise(err), () => resolve(2)), 2); | ||
await test("tryOrElse executes the second function and return the result if the first function throws error", () => { | ||
assertEquals(tryOrElse(() => raise("err"), () => 2), 2); | ||
}); | ||
|
||
await test("tryOrElse with Error (async)", async () => { | ||
type T = Promise<number>; | ||
await assertRejects( | ||
() => tryOrElse((): T => reject(err), () => reject(err)), | ||
Error, | ||
"error", | ||
); | ||
await assertRejects( | ||
() => tryOrElse((): T => reject(err), () => raise(err)), | ||
Error, | ||
"error", | ||
); | ||
await assertRejects( | ||
() => tryOrElse((): T => raise(err), () => reject(err)), | ||
Error, | ||
"error", | ||
); | ||
await test("tryOrElse throws error if the second function throws error", () => { | ||
assertThrows( | ||
() => tryOrElse((): T => raise(err), () => raise(err)), | ||
() => tryOrElse(() => raise("err1"), () => raise(new Error("err2"))), | ||
Error, | ||
"error", | ||
"err2", | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters