Skip to content

Commit

Permalink
Update promise and awaited type
Browse files Browse the repository at this point in the history
  • Loading branch information
meech-ward committed Oct 28, 2024
1 parent 5779129 commit 943d7f4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/go/mightFail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { makeProxyHandler } from "../utils/staticMethodsProxy"
import { MightFail, MightFailFunction, NotUndefined } from "../utils/utils.type"
import { mightFailFunction as standardMightFailFunction } from "../utils/mightFailFunction"

const mightFailFunction: MightFailFunction<"go"> = async function <T>(promise: Promise<T>) {
const mightFailFunction: MightFailFunction<"go"> = async function <T>(promise: T) {
const { result, error } = await standardMightFailFunction(promise)
return error
? createEither<T, "go">({ result: undefined, error }, "go")
: createEither<T, "go">({ result, error: undefined }, "go")
? createEither<Awaited<T>, "go">({ result: undefined, error }, "go")
: createEither<Awaited<T>, "go">({ result, error: undefined }, "go")
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/createEither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Either as GoEither } from "../go/Either"

// This is not how we intended the tuple feature to work but this is the only way we could currently get TypeScript to play nice
// this really should just be an interator on the either object, but it's much more complicated because of TS.
// All the details are in this PR https://github.com/meech-ward/might-fail/pull/7#issuecomment-2395122593
// All the details are in this PR https://github.com/might-fail/ts/pull/7#issuecomment-2395122593
// hopefully we can change this with a future version of TS.

export const createEither = <T, TEitherMode extends EitherMode = "standard">(
Expand Down
10 changes: 5 additions & 5 deletions src/utils/mightFailFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { createEither } from "./createEither"
import { MightFailFunction } from "./utils.type"

export const mightFailFunction: MightFailFunction<"standard"> = async function <T>(
promise: Promise<T>
): Promise<Either<T>> {
promise: T
): Promise<Either<Awaited<T>>> {
try {
const result = await promise
return createEither<T>({ result, error: undefined })
return createEither<Awaited<T>>({ result, error: undefined })
} catch (err) {
const error = handleError(err)
return createEither<T>({ error, result: undefined })
return createEither<Awaited<T>>({ error, result: undefined })
}
}
}

0 comments on commit 943d7f4

Please sign in to comment.