JSPM

async-process-limit

0.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q18951F
  • License MIT

This library can be used in the 'NodeJs', 'Browser' and 'Espruino' , but platform need support 'Promise' or 'async/await' only.

Package Exports

  • async-process-limit

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (async-process-limit) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

async-process-limit

npm

This library can be used in the NodeJs, Browser and Espruino , but platform need support Promise or async/await only.

Installation

npm install async-process-limit

How to use

require the library

const Queue = require("async-process-limit");

create example

const array=[1,2,3,4,5,6,7....]
const q=new Queue({
    list: array,
    limit: 3,
    async process(val, taskId) {
      //....
    }
});

Parameters

new Queue(options)

options

This library support follow options:

  • process: A function that will be called when a process ( Callback(cursorValue, taskId) )
  • error: A function that will be called when on error ( Callback(Error, cursorValue, tryCount, taskId) )
  • list: A array set process value list ( Default: [] )
  • start: A boolean set execute now ( Default: false )
  • limit: A number set task max length ( Default: 1 )
  • retry: A number set task retry count ( Default: 0 )

Methods

q.start()

start forEach and process

q.stop()

Stops the queue. can be resumed with q.start().

q.end()

Stop and empty the queue immediately.

q.push(item1,item2,item3...)

q.concat([item1,item2,item3...])

example

simple start :

const queue = new Queue({
  list: rows,
  start: true,
  limit: 3,
  async process(val, taskId) {
    // processing
  },
});

// or

q.start();

normal demo :

I tested it in my espruino board( ESP32 / ESP8266 ). It's OK

const queue = new Queue({
  list: array,
  start: true,
  limit: 3,
  process: (val, taskId) => {
    return new Promise((r) => {
      // processing
    });
  },
});

async function :

const queue = new Queue({
  list: array,
  start: true,
  limit: 3,
  async process(val, taskId) {
    // processing
  },
});

retry :

const queue = new Queue({
  limit: 3,
  retry: 3, //<===
  async process(val, taskId) {
    // processing
  },
  error(err, val, tryCount, taskId) {
    // error
  },
});

PromiseArray

const queue = new Queue({
  list: PromiseArray,
  limit: 3,
  start: true,
  retry: 3,
});

Push / Concat

const queue = new Queue({
  start: true,
  limit: 3,
  async process(val, taskId) {
    // processing
  },
});

queue.push(...array);
queue.concat(array);