Create and handle Promises in a simple manner. Especially useful for TCP based protocol implementations.
const PromiseStore = require("promise-store-js");
const store = new PromiseStore();
store
.create("myPromise")
.then((res) => console.log("myPromise resolved with:", res));
store.resolve(function (el) {
return el.context === "myPromise";
}, "Hello World");
This module is available through the npm registry.
$ npm install promise-store-js
- Simple Promise creation
- Configurable timeout
- Resolve Promises by using custom filter functions
Create an instance to create and handle promises.
The following table describes the properties of the optional options
object.
Property | Description | Type | Default |
---|---|---|---|
timeout | Time in milliseconds a promise times out | Number | 6000 |
This creates a Promise
and returns it.
The context
argument can be of any
type and is ment to be used for filtering.
Resolve one or more Promises
previously created.
The filter
argument has to be a function
or RegExp
. In case of a function
it should return a truthy to resolve the matching Promise
, and a falsy value otherwise.
function (el) { return el.context === 'foo' }
In case of a RegExp
, the .test()
method will be called against the context, passed in while creation.
const promise = store.create('foo')
store.resolve(/foo/,'bar')
await promise // Returns 'bar'
The value
argument can be of any
type and will be the promise result.
Returns the number of Promises
currently pending.
https://github.com/cybusio/promise-store-js/tree/main/examples