Socket
Socket
Sign inDemoInstall

easy-promise-queue

Package Overview
Dependencies
0
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    easy-promise-queue

An easy JavaScript Promise queue which is automatically executed, concurrency controlled and suspendable.


Version published
Weekly downloads
600
decreased by-32.2%
Maintainers
1
Install size
10.2 kB
Created
Weekly downloads
 

Readme

Source

easy-promise-queue

Easy promise queue. Set a concurrency to execute promises in the queue.

NPM

Build Status codecov

English 中文

What is it used for

It's a concurrent queue which can pause.

When its concurrency is set as 1(by default), it's a FIFO queue.

You can put Promises into this queue. Only X promises can be executed concurrently as your configuration.

You can pause/resume this queue at any time. When the queue is paused, ongoing promises will keep running until done though.

Installation

$ npm install easy-promise-queue

Usage

How to import

commonJS:

const PromiseQueue = require("easy-promise-queue").default;

es2015:

import PromiseQueue from 'easy-promise-queue';

How to use

Add Promise thunk to run promise one by one:
let pq = new PromiseQueue({concurrency: 1});

pq.add(() => {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      console.log('task 1');
      resolve();
    }, 1000)
  });
});

pq.add(() => {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      console.log('task 2');
      resolve();
    }, 1000)
  });
});

// syntax sugar:
pq.add([promiseThunk, promiseThunk, promiseThunk]);
// is equal to:
pq.add(promiseThunk).add(promiseThunk).add(promiseThunk);
// is equal to:
pq.add(promiseThunk);
pq.add(promiseThunk);
pq.add(promiseThunk);

//The added promises will be executed one by one.
How to pause the queue:
...
pq.pause();
// you can still add promise, however none of them will run.

pq.resume();
// Promises will resume to run.

License

MIT

Keywords

FAQs

Last updated on 01 Nov 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc