@typescript-package/promise
v1.1.0
Published
A lightweight TypeScript library for promises.
Downloads
224
Maintainers
Readme
@typescript-package/promise
A lightweight TypeScript library for promises.
Table of contents
Installation
npm install @typescript-package/promise --save-peerApi
import {
Deferred,
LazyDeferred,
LazyPromise
} from '@typescript-package/data';Concrete
Deferred
import { Deferred } from '@typescript-package/promise';
// Creates a new Deferred instance and resolves it with the value 42, then logs the resolved value to the console
const deferred = new Deferred<number>();
// Resolves the deferred with a value of 42 and logs it to the console
deferred.resolve(42);
// Attaches a then handler to the deferred that logs the resolved value to the console when the promise is fulfilled
deferred.then(value => {
console.log('Resolved value:', value);
});
// Creates a new Deferred instance and rejects it with an error, then logs the error to the console
deferred.catch(error => {
console.error('Error:', error);
});LazyDeferred
import { LazyDeferred } from '@typescript-package/promise';
// Creates a new LazyDeferred instance with a factory function that returns the value 42
const lazyDeferred = new LazyDeferred(() => 127);
// Attaches a then handler to the lazy deferred that logs the resolved value to the console when the promise is fulfilled, and a catch handler that logs any errors to the console
await lazyDeferred.then(value => {
console.log('Resolved with:', value);
}).catch(error => {
console.error('Error:', error);
});
console.log(lazyDeferred.settled); // true
console.log(lazyDeferred.value); // 127
// Attaches a finally handler to the lazy deferred that logs a message to the console when the promise is settled (either resolved or rejected)
lazyDeferred.finally(() => {
console.log('Promise settled');
});
lazyDeferred.reset(); // Resets the lazy deferred to its initial state, allowing it to be executed again with the same factory function
lazyDeferred.resolve(134); // Manually resolves the lazy deferred with the value 134, overriding the factory function
lazyDeferred.reject(new Error('Test error')); // Manually rejects the lazy deferred with an error, overriding the factory function
console.log(`lazyDeferred.settled`, lazyDeferred.settled); // true
console.log(`lazyDeferred.value`, lazyDeferred.value); // 134LazyPromise
import { LazyPromise } from '@typescript-package/promise';
const lazyPromise = new LazyPromise(() => 42);
console.log(`lazyPromise.settled`, lazyPromise.settled); // false
console.log(`lazyPromise.value`, lazyPromise.value); // undefined
// Attaches a callback that is invoked when the lazy promise is resolved.
await lazyPromise.then(value => {
console.log('Resolved value:', value); // Resolved value: 42
}).catch(error => {
console.error('Error:', error);
});
// Attaches a callback that is invoked when the lazy promise is settled (either resolved or rejected).
lazyPromise.finally(() => {
console.log('Promise settled');
});
console.log(`lazyPromise.settled`, lazyPromise.settled); // true
console.log(`lazyPromise.value`, lazyPromise.value); // undefined
// Resets the lazy promise to its initial state, allowing it to be executed again with the same factory function.
// lazyPromise.reset();Contributing
Your contributions are valued! If you'd like to contribute, please feel free to submit a pull request. Help is always appreciated.
Support
If you find this package useful and would like to support its and general development, you can contribute through one of the following payment methods. Your support helps maintain the packages and continue adding new.
Support via:
or via Trust Wallet
Thanks for your support!
Code of Conduct
By participating in this project, you agree to follow Code of Conduct.
GIT
Commit
Versioning
Given a version number MAJOR.MINOR.PATCH, increment the:
- MAJOR version when you make incompatible API changes,
- MINOR version when you add functionality in a backwards-compatible manner, and
- PATCH version when you make backwards-compatible bug fixes.
Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
FAQ How should I deal with revisions in the 0.y.z initial development phase?
The simplest thing to do is start your initial development release at 0.1.0 and then increment the minor version for each subsequent release.
How do I know when to release 1.0.0?
If your software is being used in production, it should probably already be 1.0.0. If you have a stable API on which users have come to depend, you should be 1.0.0. If you’re worrying a lot about backwards compatibility, you should probably already be 1.0.0.
License
MIT © typescript-package (license)
Packages
- @typescript-package/affix: A lightweight TypeScript library for the affix - prefix and suffix.
- @typescript-package/are: Type-safe
arecheckers for validating value types in TypeScript. - @typescript-package/descriptor: A lightweight TypeScript library for property descriptor.
- @typescript-package/guard: Type-safe guards for guarding the value types in TypeScript.c
- @typescript-package/history: A TypeScript package for tracking history of values.
- @typescript-package/is: Type-safe is checkers for validating value types in TypeScript.
- @typescript-package/map: A lightweight TypeScript library for enhanced
mapmanagement. - @typescript-package/name: A lightweight TypeScript library for the name with prefix and suffix.
- @typescript-package/property: A lightweight TypeScript package with features to handle object properties.
- @typescript-package/queue: A lightweight TypeScript library for managing various queue and stack structures.
- @typescript-package/range: A lightweight TypeScript library for managing various types of ranges.
- @typescript-package/regexp: A lightweight TypeScript library for RegExp.
- @typescript-package/set: A lightweight TypeScript library for enhanced
setmanagement. - @typescript-package/state: Simple state management for different types in TypeScript.
- @typescript-package/storage: The storage of data under allowed names.
- @typescript-package/type: Utility types to enhance and simplify TypeScript development.
- @typescript-package/wrapper: A lightweight TypeScript library to wrap the text with the opening and closing chars.
