wait-for-signals waits for a number of signals before resolving a promise.
Category | Status |
---|---|
Version | |
Dependencies | |
Dev dependencies | |
Build | |
License |
$ npm install wait-for-signals
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.
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
To run quality assurance for this module use roboter:
$ npx roboter