Skip to content

Latest commit

 

History

History
112 lines (90 loc) · 3.78 KB

README.md

File metadata and controls

112 lines (90 loc) · 3.78 KB

Concurrent Manager

npm npm GitHub issues

A simple and fast way to manage concurrent promise tasks with Queue Data Structure.

Table of Contents

Why should I use it?

Sometimes you have to do any large concurrent processing using a Promise list and you don't want to Promise.all then because it will load all the promises into memory and will stop when any error occur. This package can help you with that! You can run concurrent promise by queuing it and set if it can continue processing even if any error occur. It has zero external dependencies.

Installation

npm i concurrent-manager

Usage

Creating Instance

import ConcurrentManager from 'concurrent-manager';

const concurrent = new ConcurrentManager({
  concurrent: 10, // max concurrent process to be run
  withMillis: true // add millisecond tracing to process
});

Queueing Process

concurrent.queue(() => {
  return doSomethingPromiseRequest();
});

// or
concurrent.queue(async() => {
  const response = await doSomethingPromiseRequest();
  return response;
});

// Run all queued process
console.log(concurrent.getListedProcess());
concurrent.run()
  .then(() => {
    // You can access that fulfilled & rejected are here
    console.log(concurrent.getListedProcess());
  });

But, you can run it independently too!

const processId = concurrent.queue(() => {
  return doSomethingPromiseRequest();
});

concurrent.getProcess(processId)
  .run()
  .then((response) => {
    console.log('Response ==>', response);
  });

You can alse re-trying run promise if that has rejected.

async function main() {
  concurrent.queue(() => {
    return doSomethingPromiseRequest();
  });

  // or
  concurrent.queue(async() => {
    const response = await doSomethingPromiseRequest();
    return response;
  });

  // Run all queued promises
  await concurrent.run()

  const rejected = concurrent.getProcess('rejected');

  concurrent.getListedProcess('rejected')
    .forEach((process) => process.run());
}

Publishing

  • Before pushing your changes to Github, make sure that version in package.json is changed to newest version. Then run npm install for synchronize it to package-lock.json
  • After your changes have been merged on branch main, you can publish the packages by creating new Relase here: https://github.com/gadingnst/concurrent-manager/releases/new
  • Create new tag, make sure the tag name is same as the version in package.json.
  • You can write Release title and notes here. Or you can use auto-generated release title and notes.
  • Click Publish Release button, then wait the package to be published.

License

concurrent-manager is freely distributable under the terms of the MIT license.

Feedbacks and Issues

Feel free to open issues if you found any feedback or issues on concurrent-manager. And feel free if you want to contribute too! 😄


Built with ❤️ by Sutan Gading Fadhillah Nasution on 2022