Promise.withResolvers() wrapper and polyfill
npm install @silyze/resolverA minimal wrapper around Promise.withResolvers() with a built-in polyfill for environments that do not yet support the native API.
This utility allows you to obtain a promise, resolve, and reject object in a clean and consistent way.
- Uses native Promise.withResolvers() when available (Node 22+, modern browsers)
- Falls back to a spec-compliant polyfill when not available
- Type-safe with full TypeScript support
- Lightweight and dependency-free
- Supports both CommonJS and ES Modules
``bash`
npm install @silyze/resolver
`ts
import createResolver from "@silyze/resolver";
const { promise, resolve, reject } = createResolver
setTimeout(() => resolve(42), 1000);
const result = await promise; // 42
`
Returns an object with the following properties:
| Property | Type | Description | |
| --------- | ------------------------ | ------------------------------------------ | -------------------- |
| promise | Promise | The promise that can be awaited or chained | |resolve
| | \(value: T | PromiseLike | Resolves the promise |reject
| | (error: Error) => void | Rejects the promise | |
`ts
const { promise, resolve, reject } = createResolver
someStream.on("data", resolve);
someStream.on("error", reject);
try {
const data = await promise;
console.log("Received:", data);
} catch (err) {
console.error("Stream failed:", err);
}
`
If Promise.withResolvers() is not available, this package will fall back to an internal implementation using new Promise().
| Environment | Native Support | Works with @silyze/resolver |
| --------------- | -------------- | --------------------------- |
| Node.js 22+ | Yes | Yes (uses native) |
| Node.js 14–21 | No | Yes (uses polyfill) |
| Modern Browsers | Yes | Yes |
| Older Browsers | No | Yes |
`ts
export type Resolver
resolve: (value: T | PromiseLike
reject: (error: Error) => void;
};
export type ResolverWithPromise
promise: Promise
};
``