2.1.0 • Published 3 years 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-stateAPI
Creating state
There are 2 types of AsyncState:
manualstate can be set only manuallyeventstate 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 toundefinedhandleValue(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
valuesimple property to access currently set value
Authors
- vuwuv - Initial work - vuwuv
License
MIT License © vuwuv