UNPKG

tiny-future

Version:

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

1 lines 3.82 kB
{"version":3,"file":"main.cjs","sources":["../src/future.ts"],"sourcesContent":["/**\n * A tiny way to make `Promise` more convenient to use without any dependencies.\n *\n * Create a new Future which wraps a new `Promise`, allowing you to resolve or reject it from outside the executor.\n *\n * Inspired by C# `TaskCompletionSource`.\n *\n * @typeParam T - The type of the value that the Promise will resolve to.\n *\n * @example Basic usage\n * ```ts\n * const future = new Future<number>();\n * asyncFunc(() => {\n * future.resolve(0);\n * });\n * return future.promise;\n * ```\n *\n * @example With async/await\n * ```ts\n * const future = new Future<string>();\n * setTimeout(() => future.resolve('done'), 1000);\n * const result = await future.promise;\n * console.log(result); // 'done'\n * ```\n *\n * @example Error handling\n * ```ts\n * const future = new Future<void>();\n * future.reject(new Error('something went wrong'));\n * await future.promise.catch(err => console.error(err));\n * ```\n */\nexport class Future<T> {\n /**\n * Resolves the Promise created by the Future.\n *\n * @param value - The value to resolve the Promise with, or a Promise that resolves to the value.\n *\n * @example\n * ```ts\n * const future = new Future<string>();\n * future.resolve('success');\n * // or with a Promise\n * future.resolve(Promise.resolve('success'));\n * ```\n */\n resolve!: (value: T | PromiseLike<T>) => void;\n\n /**\n * Rejects the Promise created by the Future.\n *\n * @param reason - The reason for rejecting the Promise. Typically an Error object.\n *\n * @example\n * ```ts\n * const future = new Future<string>();\n * future.reject(new Error('something went wrong'));\n * ```\n */\n reject!: (reason?: unknown) => void;\n\n /**\n * The Promise instance created by the Future.\n *\n * Use this to await the result or attach `.then()` / `.catch()` handlers.\n *\n * @example\n * ```ts\n * const future = new Future<number>();\n * future.promise.then(value => console.log(value));\n * future.resolve(42); // logs: 42\n * ```\n */\n promise: Promise<T>;\n\n /**\n * Creates a new Future instance.\n *\n * Uses `Promise.withResolvers()` if available (ES2024+), otherwise falls back to manual implementation\n * for compatibility with older environments.\n */\n constructor() {\n // If the environment supports `Promise.withResolvers`, just use it.\n if (typeof Promise.withResolvers === 'function') {\n const { promise, resolve, reject } = Promise.withResolvers<T>();\n this.promise = promise;\n this.resolve = resolve;\n this.reject = reject;\n } else {\n // Fallback for older environments\n this.promise = new Promise<T>((resolve, reject) => {\n this.resolve = resolve;\n this.reject = reject;\n });\n }\n }\n}\n"],"names":[],"mappings":";;;;AAiCO,MAAM,MAAA,CAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcnB,OAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaA,MAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcA,OAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,WAAA,GAAc;AAEV,IAAA,IAAI,OAAO,OAAA,CAAQ,aAAA,KAAkB,UAAA,EAAY;AAC7C,MAAA,MAAM,EAAE,OAAA,EAAS,OAAA,EAAS,MAAA,EAAO,GAAI,QAAQ,aAAA,EAAiB;AAC9D,MAAA,IAAA,CAAK,OAAA,GAAU,OAAA;AACf,MAAA,IAAA,CAAK,OAAA,GAAU,OAAA;AACf,MAAA,IAAA,CAAK,MAAA,GAAS,MAAA;AAAA,IAClB,CAAA,MAAO;AAEH,MAAA,IAAA,CAAK,OAAA,GAAU,IAAI,OAAA,CAAW,CAAC,SAAS,MAAA,KAAW;AAC/C,QAAA,IAAA,CAAK,OAAA,GAAU,OAAA;AACf,QAAA,IAAA,CAAK,MAAA,GAAS,MAAA;AAAA,MAClB,CAAC,CAAA;AAAA,IACL;AAAA,EACJ;AACJ;;;;"}