@esfx/async-lazy
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

@esfx/async-lazy

Lazy-initialized asynchronous value.

Overview

Installation

npm i @esfx/async-lazy

Usage

import { AsyncLazy } from "@esfx/async-lazy";

async function main() {
    // lazy initialize an object
    const lazy1 = new AsyncLazy(() => new SomeObject());
    lazy1.isStarted; // false
    const p1 = lazy1.value; // Promise {}
    lazy1.isStarted; // true
    await p1; // SomeObject {}

    // lazy initialize with arguments
    const lazy2 = Lazy.from(
        async (a, b) => (await a) + (await b),
        Promise.resolve(1),
        Promise.resolve(2));
    lazy2.isStarted; // false
    const p2 = lazy2.value; // Promise {}
    lazy2.isStarted; // true
    await p2; // 3

    // initialized "lazy"
    const lazy3 = Lazy.for(Promise.resolve("test"));
    lazy3.isStarted; // true
    await lazy3.value; // "test"
}

main().catch(e => console.error(e));

API

You can read more about the API here.

Readme

Keywords

none

Package Sidebar

Install

npm i @esfx/async-lazy

Weekly Downloads

37

Version

1.0.0

License

Apache-2.0

Unpacked Size

32.3 kB

Total Files

8

Last publish

Collaborators

  • rbuckton