2.2.2 • Published 1 year ago

lazy-var v2.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

lazy-var.js

GitHub Workflow Status npms.io (quality) npm bundle size GitHub code size in bytes npm GitHub

Lazy initialize values

Idea

A way to lazy-initialize data. Executes the initializer first time calling .get() and returns the same value afterwards. Supports both synchronous and asynchronous initializers.

The solution is inspired by System.Lazy from dotnet

Install

npm

lazy-var @ npm

or install with npm cli

dev@dingdong:~/my-project$ npm install lazy-var

or yarn

dev@dingdong:~/my-project$ yarn add lazy-var

Usage

Try it on runkit

// Setup lazy variable
import { lazy } from "lazy-var";
import { heavyMethod } from "./lib/stuff.js";

var lazyData = lazy(async () => {
    console.log("Request for data");
    return await heavyMethod()
});

// Later on: Retrieve the data

var data = await lazyData.get();
// outputs: "Request for data"
// returns: whatever data from heavyMethod

var data2 = await lazyData.get();
// no output
// returns: same data as data
2.2.2

1 year ago

2.0.2

2 years ago

2.0.5

2 years ago

2.1.0

2 years ago

2.0.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago