ensurePromise()
ensurePromise<
T>(schema,promise):ResultAsync<T,string>
Resolves a Promise, validates the result against a Kanon schema,
and returns a ResultAsync.
Eliminates the ResultAsync.fromPromise(...).andThen(...) boilerplate
when you need to fetch data and validate it in one step.
Type Parametersβ
T: Tβ
The expected output type of the schema.
Parametersβ
schema: Schema<T>β
Kanon schema to validate against.
promise: Promise<unknown>β
Promise that resolves to the value to validate.
Returns: ResultAsync<T, string>β
ResultAsync<T, string> β Ok<T> if the promise resolves and validation succeeds, Err<string> otherwise.
Sinceβ
2.2.0
Exampleβ
import { ensurePromise } from "pithos/bridges/ensurePromise";
import { object, string, number } from "pithos/kanon";
const UserSchema = object({ name: string(), age: number() });
ensurePromise(UserSchema, fetch("/api/user").then(r => r.json()))
.map(user => user.name.toUpperCase());