LazyPromise
@ai-zen/lazy-promise is a lazy Promise (LazyPromise) container implemented in TypeScript. It postpones the execution timing of the executor from "when the Promise is created" to "when it is first consumed".
Problem it solves
The native new Promise(executor) executes the executor immediately upon creation. But in certain scenarios (such as: pre-registering callbacks without knowing whether they will be used, on-demand resource initialization, conditional tasks), we prefer executing only when it is actually used. LazyPromise is designed for this purpose.
Core concepts
- Lazy execution: The executor is not executed when creating the instance; the internal
Promiseis initialized only whenthen/catch/finallyis called. - Single initialization: The internal
Promiseis initialized only once on first consumption; subsequent calls tothen/catch/finallyall reuse the same underlying Promise. - Standard Promise API: Exposes
then/catch/finallyconsistent with the native Promise, seamlessly interoperable withawaitand Promise chains.
Environment requirements
- A Node.js environment that supports ESM (
import) (the packagetypeismoduleand the entry isdist/index.js). - Note:
package.jsondoes not declare anenginesfield, so the specific Node version is not fixed. Requires manual review for the recommended minimum version.
Installation
bash
npm install @ai-zen/lazy-promiseQuick example
js
import LazyPromise from "@ai-zen/lazy-promise";
const lazy = new LazyPromise((resolve) => {
// Only executed when accessing lazy.then(...) / lazy.catch(...) / lazy.finally(...)
console.log("executor executes");
resolve("hello");
});
console.log("printed immediately after creation"); // output first; the executor is not executed here
await lazy; // triggers lazy initializationReferences
License
MIT