1.1.1 • Published 1 year ago

promise-store-js v1.1.1

Weekly downloads
-
License
GPL-3.0-or-later
Repository
github
Last release
1 year ago

Promise-Store-JS

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");

Install

This module is available through the npm registry.

$ npm install promise-store-js

Features

  • Simple Promise creation
  • Configurable timeout
  • Resolve Promises by using custom filter functions

API

new PromiseStore(options)

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 |

Methods

PromiseStore.create(context)

This creates a Promise and returns it.

The context argument can be of any type and is ment to be used for filtering.

PromiseStore.resolve(filter, value)

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.

Properties

PromiseStore.size

Returns the number of Promises currently pending.

Examples

https://github.com/cybusio/promise-store-js/tree/main/examples