Coder Social home page Coder Social logo

leehyunggeun / easy-promise-queue Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chenzhihao/easy-promise-queue

0.0 1.0 0.0 69 KB

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

JavaScript 100.00%

easy-promise-queue's Introduction

easy-promise-queue

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

NPM

Build Status

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:

require ('easy-promise-queue');

ES6:

// ES6:
import PromiseQueue from 'easy-promise-queue';

Use it directly in browse:

<script type="text/javascript" src="../dist/PromiseQueue.js"></script>
...
</script>

Have a try:

Jsbin Demo

### How to use:
#### Add Promise thunk to run promise one by one:
```js

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

easy-promise-queue's People

Contributors

chenzhihao avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.