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());