wait-for-signals
TypeScript icon, indicating that this package has built-in type declarations

1.2.7 • Public • Published

wait-for-signals

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

Status

Category Status
Version npm
Dependencies David
Dev dependencies David
Build GitHub Actions
License GitHub

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

Readme

Keywords

none

Package Sidebar

Install

npm i wait-for-signals

Weekly Downloads

2

Version

1.2.7

License

MIT

Unpacked Size

11.4 kB

Total Files

13

Last publish

Collaborators

  • thenativeweb-admin
  • goloroden