@sekhmet/vow
Vow is a promise-like value that resolves synchronously. A vow is Pending until something resolves it, then Resolved with its value. Callbacks attached with map or zip run inside the resolve call, not on a later microtask, so a value flows through a chain of vows in one call stack. wait and fromPromise bridge to promises.
Install
The package is published on JSR.
sh
deno add jsr:@sekhmet/vowFor Node.js, Bun and other npm-compatible runtimes, install through JSR's npm compatibility layer:
sh
npx jsr add @sekhmet/vow # npm
pnpm dlx jsr add @sekhmet/vow # pnpm
yarn dlx jsr add @sekhmet/vow # yarn
bunx jsr add @sekhmet/vow # bunExample
ts
import { assertEquals } from "@std/assert";
import { map, vowWithResolver, wait, zipWith } from "@sekhmet/vow";
const { vow: price, resolve: setPrice } = vowWithResolver<number>();
const { vow: quantity, resolve: setQuantity } = vowWithResolver<number>();
const seen: number[] = [];
const total = zipWith(price, quantity, (p, q) => p * q);
map(total, (value) => seen.push(value));
setPrice(5);
assertEquals(seen, []); // still waiting for the quantity
setQuantity(3);
assertEquals(seen, [15]); // ran synchronously inside setQuantity
assertEquals(await wait(total), 15);API
@sekhmet/vow exports free functions; @sekhmet/vow/mini exports the same set.
| Export | Does |
|---|---|
new Vow(executor) | a vow the executor resolves, now or later |
vowWithResolver() | a pending vow and the function that resolves it |
resolved(value) | a vow already resolved with value |
map(vow, f) | a vow of f(value) |
zip(a, b), zipWith(a, b, f) | a vow of both values once both resolve |
wait(vow) | a Promise of the value |
fromPromise(promise) | a vow of Ok(value) or Err(reason) once the promise settles |
A vow resolves once; later calls to its resolver do nothing. If dependents throw while a vow resolves, the rest still run and the resolve call throws an AggregateError of their errors.
Learn more
- Guide: getting started, also on the docs site.
- API reference: jsr.io/@sekhmet/vow.
License
MIT. See LICENSE.md.