1.2.7 • Published 3 years ago

wait-for-signals v1.2.7

Weekly downloads
186
License
MIT
Repository
github
Last release
3 years ago

wait-for-signals

wait-for-signals waits for a number of signals before resolving a promise.

Status

CategoryStatus
Versionnpm
DependenciesDavid
Dev dependenciesDavid
BuildGitHub Actions
LicenseGitHub

Installation

$ npm install wait-for-signals

Quick Start

First you need to add a reference to wait-for-signals to your application:

const { waitForSignals } = require('wait-for-signals');

If you use TypeScript, use the following code instead:

import { waitForSignals } from 'wait-for-signals';

Then, to wait for a number of signals, call the waitForSignals function and hand over the desired number of signals to wait for:

const collector = waitForSignals({ count: 3 });

To notify the collector of a signal, use its signal function:

await collector.signal();

Finally, to wait until all signals have occured, wait for the collector's promise property to resolve:

await collector.promise;

E.g., if you want to wait for three requests being sent to your API and then do something, you may use the following code:

const collector = waitForSignals({ count: 3 });

app.get('/', async (req, res) => {
  await collector.signal();

  res.status(200).end();
});

collector.promise.then(() => {
  // ...
});

All subsequent API calls will increase a counter within the collector, but apart from that nothing will happen.

Getting the counter value

To get the current counter value use the getCount function:

const collector = waitForSignals({ count: 3 });

console.log({ count: collector.getCount() });
// => 0

await collector.signal();

console.log({ count: collector.getCount() });
// => 1

Running quality assurance

To run quality assurance for this module use roboter:

$ npx roboter
1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago