1.4.4 • Published 7 years ago

pending-promise-set v1.4.4

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
7 years ago

pending-promise-set

Travis npm npm

A self-managing collection of pending promises

Installation

npm install pending-promise-set

Usage

const PendingPromiseSet = require('pending-promise-set');

Properties

get size

Get the size of the set.

console.log(set.size);
  • returns Number - The number of contained promises.

Events

A PendingPromiseSet extends the class exported by the events package and emits the following events with the specified parameters:

resolve(value, promise)

Called when a promise from the set has been resolved.

  • value <any> - The resolved value.
  • promise Promise - The promise that has been resolved.

reject(error, promise)

Called when a promise from the set has been rejected.

  • error <any> - The rejected error.
  • promise Promise - The promise that has been rejected.

delete(promise)

Called when a promise has been deleted from the set using set.delete(..).

  • promise Promise - The promise that has been deleted.

clear()

Called when the set has been cleared using set.clear(..).

Functions

new PendingPromiseSet()

Create an empty pending promise set.

let set = new PendingPromiseSet();

set.add(promise)

Add a promise to the set. When the promise is resolved or rejected it will be deleted from the set.

let promise = set.add(new Promise(resolve => {
	setTimeout(resolve, 500);
}));
  • promise Promise | <null> | <undefined> - The promise to add. null, undefined or duplicate promises are ignored.
  • returns Promise - The promise that was passed to .add(..)

set.handle(getResults)

Create a promise that resolves or rejects as soon as the set is empty. After the set is empty, the promise will reject an array with errors or will resolve to an array with all results if no errors have occured.

set.add(someAction());
set.add(someOtherAction());
let results = await set.handle();
  • getResults boolean - If falsy, no results are recorded and and the resolved array will be empty. Defaults to true.

set.join(...promises)

Create a promise that resolves as soon as the set is empty. Note that results or errors are not handled by this function.

set.add(someAction());
set.add(someOtherAction());
await set.join();
  • promises Promise... - Optional promises to add before.
  • returns Promise

set.has(promise)

Check if the set has the specified promise.

let hasPromise = set.has(myPromise);
  • promise <any> - The promise to test for.
  • returns boolean

set.delete(promise)

Delete a promise from the set.

let promise = set.add(someAction());
await someOtherAction();
set.delete(promise);
  • promise <any> - The promise to delete.
  • returns boolean - True, if the promise has been removed, otherwise false.

set.clear()

Delete all promises from the set.

set.clear();

set.captureErrors()

Utility for handling errors later.

let handleErrors = set.captureErrors();

set.add(await someOtherAction());
await set.join();

// Throw errors if one or more:
handleErrors();
  • returns Function(handler) - A function to handle errors. If called, no more errors will be captured and an optional handler is called with a Set of errors that were rejected since the call of set.captureErrors(). If no handler is provided, a useful Error is thrown with a errors property which is an array of all errors.
1.4.4

7 years ago

1.4.3

8 years ago

1.4.2

8 years ago

1.4.1

8 years ago

1.4.0

8 years ago

1.3.1

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago