Skip to content
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

feat(core): handle results in fromPromise/fromSafePromise #45

Merged
merged 1 commit into from
Feb 16, 2025
Merged
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
30 changes: 23 additions & 7 deletions core/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -834,11 +834,19 @@ export function errAsync<T = unknown, E = never>(
* You can optionally pass `errorFn` as the second argument to map rejected
* error from `unknown` to `E`.
*/
export function fromPromise<T, E>(
promise: Promise<T>,
errorFn?: ErrFn<unknown, E>,
): AsyncResult<T, E> {
return chain(promise.then(ok).catch((e) => err(errorFn ? errorFn(e) : e)));
export function fromPromise<T, E1, E2>(
promise: Promise<T | Result<T, E1>>,
errorFn?: ErrFn<unknown, E2>,
): AsyncResult<T, E1 | E2> {
return chain(
promise.then((v) => {
if (v instanceof Ok || v instanceof Err) {
return v;
}

return ok(v);
}).catch((e) => err(errorFn ? errorFn(e) : e)),
);
}

/**
Expand All @@ -848,8 +856,16 @@ export function fromPromise<T, E>(
* **Ensure you know what you're doing, otherwise a thrown exception within this
* promise will cause `AsyncResult` to reject.**
*/
export function fromSafePromise<T>(promise: Promise<T>): AsyncResult<T, never> {
return chain(promise.then(ok));
export function fromSafePromise<T, E = never>(
promise: Promise<T | Result<T, E>>,
): AsyncResult<T, E> {
return chain(promise.then((v) => {
if (v instanceof Ok || v instanceof Err) {
return v;
}

return ok(v);
}));
}

/**
Expand Down
24 changes: 24 additions & 0 deletions core/result_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ Deno.test("errAsync", async () => {

Deno.test("fromPromise", async () => {
assertEquals(await fromPromise(Promise.resolve(1)).unwrap(), 1);
assertEquals(
await fromPromise(Promise.resolve(ok(4))).unwrap(),
4,
);
assertEquals(
await fromPromise(Promise.resolve(err("qwe"))).unwrapErr(),
"qwe",
);
assertEquals(
await fromPromise(Promise.resolve(okAsync(5))).unwrap(),
5,
);

assertEquals(
await fromPromise(Promise.reject("rejected error")).unwrapErr(),
Expand All @@ -102,6 +114,18 @@ Deno.test("fromPromise", async () => {

Deno.test("fromSafePromise", async () => {
assertEquals(await fromSafePromise(Promise.resolve(1)).unwrap(), 1);
assertEquals(
await fromSafePromise(Promise.resolve(ok(4))).unwrap(),
4,
);
assertEquals(
await fromSafePromise(Promise.resolve(err("qwe"))).unwrapErr(),
"qwe",
);
assertEquals(
await fromSafePromise(Promise.resolve(okAsync(5))).unwrap(),
5,
);

await assertRejects(
async () =>
Expand Down