smart-resource
TypeScript icon, indicating that this package has built-in type declarations

2.2.0 • Public • Published

Smart Resource

An observable helper for asynchronous resources

Installation

npm install smart-resource

Usage

A SmartResource takes any function that returns a promise and converts it into an observable that automatically notifies subscribers when a refetch is triggered.

const RandomNumberResource = new SmartPromise(() => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve(Math.random());
        }, 1000);
    });
});

const subscriber = RandomNumberResource.subscribe((resource) => {
    console.log(`New random number: ${resource.value}!`);
});

RandomNumberResource.fetch();
RandomNumberResource.fetch();

subscriber.unsubscribe();

RandomNumberResource.fetch();

Output:

New random number: 0.5081273919151901!
New random number: 0.44005522329031255!

Package Sidebar

Install

npm i smart-resource

Weekly Downloads

15

Version

2.2.0

License

Apache-2.0

Unpacked Size

69 kB

Total Files

27

Last publish

Collaborators

  • ericmantooth