@jovulic/lazy-promise v1.2.2
Disclaimer
This repository is a personal project created for practicing the process of building and publishing an open-source library.
LazyPromise
A lazy promise that waits until you ask it to get to work.
π Description
LazyPromise is a lightweight utility for lazily initializing values in JavaScript and TypeScript. Specifically, it will not perform any computation until the value is accessed (e.g., awaited). Additionally, LazyPromise supports lazy chaining, allowing you to chain operations that also wonβt execute until the result is needed.
β¨ Features
β
Lazy evaluation β Compute values only when first accessed.
β
Lazy Chaining β Define transformations to values lazily.
β
Minimal β No dependencies.
β
TypeScript support β Fully typed.
π¦ Installation
Using npm:
npm install @jovulic/lazy-promiseUsing yarn:
yarn add @jovulic/lazy-promiseUsing pnpm:
pnpm add @jovulic/lazy-promiseπ Usage
Basic Example
import { LazyPromise } from "@jovulic/lazy-promise";
const lazyValue = new LazyPromise(async () => {
return "My Lazy Value";
});
(async () => {
console.log(await lazyValue); // "My Lazy Value"
console.log(await lazyValue); // "My Lazy Value" (no recomputation)
})();Lazy Chaining
import { LazyPromise } from "@jovulic/lazy-promise";
const lazyValue = new LazyPromise(async () => {
return "My Lazy Value";
});
const transformedValue = lazyValue.later((value) => value.toUpperCase());
console.log(await transformedValue); // "MY LAZY VALUE"π οΈ Build
This project uses Nix for development to ensuring a consistent and reproducible environment. It is easy enough to build without it, but the following guide will be using Nix.
Follow these steps to build and work on the project locally:
Install Nix: If you don't have Nix installed, follow the instructions for your platform at https://nixos.org/download.html.
Clone the Repository: Clone the
lazy-promiserepository to your local machine.git clone https://github.com/jovulic/lazy-promise.git cd lazy-promiseEnter the Development Shell: Use the following command to enter the Nix development shell. This will automatically install all the necessary dependencies defined in the
flake.nixfile.nix developThis command might take a while the first time as it downloads and installs the dependencies. Subsequent entries into the shell will be much faster.
Install NPM Dependencies: Once inside the Nix shell, you'll need to install the project's npm dependencies. Even though Nix provides Node.js and npm, the project dependencies are managed by npm. We do this via the
ctlcommand that is added into the development shell.ctl setupBuild the Library: You can now build the library.
ctl buildThis will create a
distdirectory containing the compiled library files.