New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

stateful-value

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stateful-value

Values with built-in states. Suitable for async operations.

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

stateful-value: Values with built-in states

Please see ARTICLE.md for the rationale of the package.

Installation

npm i -S stateful-value

Usage

To represent a value that could be:

  • Unfulfilled: it has not been loaded/populated (e.g. AJAX, user input)
  • Error: error encountered during loading operation
  • Valid: it has been successfully loaded
import { StatefulValue } from "stateful-value";

let data: StatefulValue<string>;
if (isValid(data)) {
    // data is guaranteed to be a string
    console.log(data.length);
}

data = await loadData();
if (isError(data)) {
    // data is guaranteed to be an Error (or subclass of Error) object
    console.error(data.message);
}

API

Type StatefulValue<T>

Defines a type that can represent a value that is either unfulfilled, or is an error, or is valid.

Function isUnfulfilled(): boolean

Determines whether the value is undefined or an Unfulfilled object.

Function isError(): boolean

Determines whether the value is an Error object.

Function isValid(): boolean

Determines whether the value is valid (i.e. an instance of type T).

Function isNonNull(): boolean

Determines whether the value is valid and is not null. Note: null is considered a valid value if T contains it, e.g. StatefulValue<string | null>.

Function getValue(): T | undefined

Returns the value if it is valid; otherwise, returns undefined.

Function getError(): T | undefined

Returns the value if it is an Error object; otherwise, returns undefined.

Function resolveValue

async function resolveValue<T>(
    callback: () => Promise<T>,
    dependencies: StatefulValue<unknown>[] = []
): Promise<StatefulValue<T>>

Resolves a stateful value when all dependencies are valid.

  • If any of the dependencies is an error, then return the first dependency error.
  • If all dependencies are valid, then invoke the callback to get the target stateful value.
  • Otherwise, returns Unfulfilled.

Keywords

stateful

FAQs

Package last updated on 19 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts