LazyPromise is a TypeScript class that represents a lazy promise, which executes its executor function only when then, catch, or finally methods are called.
Install LazyPromise using npm:
npm install @ai-zen/lazy-promiseImport LazyPromise class into your project:
import LazyPromise from "@ai-zen/lazy-promise";Create a new instance of LazyPromise by passing an executor function:
const lazyPromise = new LazyPromise((resolve, reject) => {
// Perform asynchronous operations and then call resolve or reject
});Use the then method to attach fulfillment and rejection handlers:
lazyPromise.then(
(value) => {
// Handle the fulfilled promise
},
(reason) => {
// Handle the rejected promise
}
);Use the catch method to attach a rejection handler:
lazyPromise.catch((reason) => {
// Handle the rejected promise
});Use the finally method to attach a handler when the promise is settled:
lazyPromise.finally(() => {
// Perform cleanup or other operations
});Note: The executor function will only be executed when one of the above methods is called.
This project is licensed under the MIT License.