2.1.0 • Published 1 year ago
@async-cancellables/async-state v2.1.0
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 manuallyevent
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 iswaitEmpty(cancellationToken = null)
waits for the value to be cleared back toundefined
handleValue(handler, cancellationToken = null)
waits for the value to be set to any defined value if not yet and returns the result ofhandler(value)
Accessing current value
value
simple property to access currently set value
Authors
- vuwuv - Initial work - vuwuv
License
MIT License © vuwuv