pending-promise-set v1.4.4
pending-promise-set
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
- Thepromise
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 totrue
.
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 toadd
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 aSet
of errors that were rejected since the call ofset.captureErrors()
. If no handler is provided, a usefulError
is thrown with aerrors
property which is an array of all errors.