2.1.0 • Published 6 years ago

seq-prom v2.1.0

Weekly downloads
1,618
License
ISC
Repository
github
Last release
6 years ago

SeqProm

Repo for Sequential Promises project

Installation

npm install seq-prom --save

Options

OptionTypeDescriptionDefaultRequired?
listArrayList of items to iterate throughYes
cbFunctionFunction called when time to process next itemYes
batchSizeIntegerSize of the batch, or if using stream mode, how many streams to use1No
errorCBFunctionCalled when there is an error, with the item and reason for errorNo
finalCBFunctionCalled when all is done. Passes in a list of errors, and any items, passed to the resolve methodNo
useStreamBooleanWill switch to stream mode, which meaning multiple queues are created with the listfalseNo
autoStartBooleanInstead of having to call .start() will do this for youfalseNo

Usage

Basic Example

const SeqProm = require("seq-prom");

let seqProm = SeqProm({
  list: [1, 2, 3],
  cb  (item, resolve, reject) {
    console.log(`Item [${item}] called!`);
    setTimeout(function () {
      if (item === 3) {
        return reject("Not sure about this!");
      } else {
        return resolve();
      }
    }, item * 1000);
  },
  errorCB (item, reason) {
    console.error(`Item [${item}] failed with error: ${reason}`);
  },
  finalCB (){
    console.log("All done!");
  }
});

seqProm.start();

Other Options

const SeqProm = require("seq-prom");

SeqProm({
  list: [1, 2, 3, 4],
  autoStart: true,
  useStream : true,
  batchSize : 2,
  cb  (item, resolve, reject) {
    console.log(`Item [${item}] called!`);
    setTimeout(function () {
      if (item === 3) {
        return reject("Not sure about this!");
      } else {
        return resolve(item);
      }
    }, item * 1000);
  },
  errorCB (item, reason) {
    console.error(`Item [${item}] failed with error: ${reason}`);
  },
  finalCB (errors, responses){
    console.log("All done!");
    console.dir(errors);
    console.dir(responses);
  }
})
.promise
.then(function(){
  console.log("Called after all is said and done");
});

Functions

cb

ArgumentDescription
itemItem from the list
resolverThe resolve function from the promise
rejecterThe reject function from the promise
selfthis passed through. Can call .stop() on this
threadIdIf in stream mode, then this will be the threadId

errorCB

ArgumentDescription
itemItem from the list
reasonReason for the error, either from the reject method or a caught error

finalCB

ArgumentDescription
errorList of errors that have occured
responseList of items passed back to the resolve function

Promise Response

const SeqProm = require("seq-prom");

SeqProm({
  list: [1, 2, 3],
  autoStart : true,
  cb  (item, resolve, reject) {
    console.log(`Item [${item}] called!`);
    setTimeout(function () {
      if (item === 3) {
        return reject("Not sure about this!");
      } else {
        return resolve(item);
      }
    }, item * 1000);
  },
  errorCB (item, reason) {
    console.error(`Item [${item}] failed with error: ${reason}`);
  },
  finalCB (errors, response){
    console.log("All done!");
  }
})
.promise
.then(([errors, responses])=> console.log(errors, responses));         

Tests

npm test

Release History

  • 1.1.1 Package update
  • 1.1.0 Updates to allow passage of data Added more tests Added autStart option Added ability to chain off of promise
  • 1.0.0 Initial release
2.1.0

6 years ago

2.0.1

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

7 years ago