npm.io
0.1.2 • Published 8 years ago

p-q

Licence
ISC
Version
0.1.2
Deps
0
Vulns
0
Weekly
0

p-q

Tiny promise fifo queue

Build Status

npm i --save p-q

Usage

Pass in a processing function that returns a promise to the constructor.


const PQ = require('p-q');

const q = PQ((msg) => {
  return new Promise(function(resolve, reject) {
    setTimeout(function () {
      resolve(msg);
    }, 1000);
  });
});

Then add some data to the queue:


q.add({
  foo: 'hii',
  bar: 'world'
})

Get events back:


q.on('processed', data => {
  console.log(`${JSON.stringify(data)} was processed`);
})

q.on('empty', _ => {
  console.log('queue is empty');
})

q.on('error', err => {
  console.error(err);
})

q.on('add', data => {
  console.log('new event added', data);
})