queue-event-emitter
QueueEventEmitter
is a simple EventEmitter
which runs every event handler in a queue.
Installation
npm i --save queue-event-emitter
Usage
const QueueEventEmitter = require('queue-event-emitter')
const sleep = (time) => (
new Promise((resolve) => setTimeout(resolve, time))
)
const emitter = new QueueEventEmitter()
emitter.on('first', async (data) => {
await sleep(3000)
console.log('data:', data)
})
emitter.on('second', async (data) => {
await sleep(1000)
console.log('data:', data)
})
emitter.on('third', async (data) => {
await sleep(10)
console.log('data:', data)
})
emitter.emit('first', 10)
emitter.emit('second', 20)
emitter.emit('third', 30)
emitter.emit('first', 40)
emitter.emit('third', 50)
emitter.emit('third', 60)
emitter.emit('second', 70)
API
Implements the same api as node's EventEmitter.
Options
QueueEventEmitter
class accepts following options:
options.concurrency
<number>
The maximum number of events to execute at once. (Default: 1)
Licence
Licensed under MIT