@async-cancellables/async-state

2.1.0 • Public • Published

Async Cancellables / AsyncState

Asynchronous class to track presence or absence of a value with builtin support of cancellation tokens

Table of contents

Prerequisites

This project requires NodeJS (version 18 or later) and NPM.

Installation

To install and set up the library, run:

$ npm install @async-cancellables/async-state

API

Creating state

There are 2 types of AsyncState:

  • manual state can be set only manually
  • event state sets value after specified event on target object is fired, state value is set to the result of handler call with event arguments
import { AsyncState } from 'async-cancellables';

const manualState = AsyncState.manual(value = undefined);
const eventState = AsyncState.event(target, event, handler);

Waiting for the value

  • wait(cancellationToken = null) waits for the value to be set to any defined value or returns it if already is
  • waitEmpty(cancellationToken = null) waits for the value to be cleared back to undefined
  • handleValue(handler, cancellationToken = null) waits for the value to be set to any defined value if not yet and returns the result of handler(value)

Accessing current value

  • value simple property to access currently set value

Authors

  • vuwuv - Initial work - vuwuv

License

[MIT License] © vuwuv

Package Sidebar

Install

npm i @async-cancellables/async-state

Weekly Downloads

0

Version

2.1.0

License

MIT

Unpacked Size

6.59 kB

Total Files

3

Last publish

Collaborators

  • vuwuv