A zero-dependency Future/Promise wrapper to resolve or reject a Promise outside its executor.
npm install tiny-future





A zero-dependency Future/Promise wrapper to resolve or reject a Promise outside its executor.
Inspired by C# TaskCompletionSource.
- Zero dependencies
- TypeScript first with full type support
- Works with Promise.withResolvers (ES2024) with automatic fallback
- Supports ESM, CommonJS, and Deno/JSR
``shnpm
npm install tiny-future
Usage
`ts
import { Future } from 'tiny-future';function sleep(ms: number): Promise {
const future = new Future();
setTimeout(() => {
// resolve/reject from anywhere, not just inside the executor
future.resolve(ms);
}, ms);
return future.promise;
}
await sleep(1000);
`$3
With
Future, you can resolve or reject from anywhere:`ts
// Using Future
const future = new Future();
someAsyncOperation((result) => {
future.resolve(result);
});
return future.promise;
`With standard
Promise, resolve/reject must be inside the executor:`ts
// Using Promise
return new Promise((resolve) => {
someAsyncOperation((result) => {
resolve(result);
});
});
`$3
`ts
const future = new Future();future.promise.catch((err) => {
console.error('Error:', err.message);
});
future.reject(new Error('something went wrong'));
`API
$3
| Property | Type | Description |
|----------|------|-------------|
|
promise | Promise | The underlying Promise instance |
| resolve | (value: T \| PromiseLike | Resolves the Promise |
| reject | (reason?: unknown) => void` | Rejects the Promise |MIT