@thuum/promising
Installation
Section titled “Installation”npm install @thuum/promisingOverview
Section titled “Overview”@thuum/promising provides modern async patterns for JavaScript/TypeScript:
withResolvers<T>()— Create a promise with externally accessible resolve/reject functionscreateContext(options)— Sequential async execution context with lifecycle monitoring
withResolvers<T>()
Section titled “withResolvers<T>()”Creates a promise along with its resolve and reject functions, making it easier to work with promises in scenarios where you need external control over resolution.
Based on the Promise.withResolvers() proposal.
import { withResolvers } from "@thuum/promising";
const { promise, resolve } = withResolvers<number>();setTimeout(() => resolve(42), 100);const value = await promise; // 42Use Cases
Section titled “Use Cases”- Event-based patterns where resolution happens in a different scope
- Testing async flows with controlled timing
- Bridging callback-based APIs to promises
createContext(options)
Section titled “createContext(options)”Creates an async execution context that ensures tasks run sequentially in the order they were enqueued, regardless of when they actually complete.
Parameters
Section titled “Parameters”options.watch?— Optional callback to monitor task lifecycle events (waiting,pending,resolved,rejected)
import { createContext, withResolvers } from "@thuum/promising";
const ctx = createContext({ watch(event) { console.log(`[${event.type}] ${event.name} (queue: ${event.size})`); },});
// Tasks execute in order, even if resolved out of orderconst r1 = withResolvers<number>();const r2 = withResolvers<number>();
const p1 = ctx.run("first", () => r1.promise);const p2 = ctx.run("second", () => r2.promise);
r2.resolve(2); // resolves second first...r1.resolve(1); // ...but "first" still runs before "second"
await p1; // 1await p2; // 2AsyncContextEvent
Section titled “AsyncContextEvent”type AsyncContextEvent = { type: "waiting" | "pending" | "resolved" | "rejected"; name: string; size: number; taskId: number;};