Giter VIP home page Giter VIP logo

best-queue's Introduction


license downloads size issues npm

English|简体中文

Introduction

best-queue let you control tasks in a queue.

It's easy to execute task one by one with interval time in queue like this:

Queue -> task -> wait(interval) -> task -> wait(interval) -> task -> finish

How about adding async task in queue:

Queue -> Promise.resolve(task) -> wait(interval) -> Promise.resolve(task) -> wait(interval) -> Promise.resolve(task) -> finish

What if we want to execute two tasks at the same time to support concurrency in queue:

Queue -> Promise.all([Promise.resolve(task), Promise.resolve(task)]) -> wait(interval) -> Promise.all([Promise.resolve(task), Promise.resolve(task)]) -> wait(interval) -> Promise.all([Promise.resolve(task), Promise.resolve(task)]) -> finish

But if one async task takes too much time because of network reason, the batch of task need to wait until the slow task resolve, we can do something make queue more efficient in theory.

That's best-queue do. See image below.

Install

type in the command line to install with:

npm i best-queue

Usage

Import as an ES Module:

import { createQueue } from "best-queue";

Require in Node:

const { createQueue } = require("best-queue");

Use Script source(Exposed BQueue as global var):

<script src="https://cdn.jsdelivr.net/npm/best-queue"></script>
<script src="https://unpkg.com/best-queue"></script>

API

Attribute/Method Description Type Default
createQueue create a queue (tasks: unkonwn[], options: Options) => Promise
options create a queue by options Object {
max: 1,
interval: 0,
recordError: false
}
options.max max concurrence task at the same time, default and min to 1 Number 1
options.interval the interval time between tow tasks(milliscond), default to 0 Number 0
options.recordError record error task instead of reject queue when task gone error Boolean false
pause() pause the queue, queue stop to execute task Function(): void
resume() rerun the queue Function(): void
subscribe(listener: Listener) listener fired a task done Function(({taskStatus: 'success' | 'error', data: unknown, taskIndex: number, progress: number}) => void): () => void

Example

import { createQueue } from "best-queue";

// simulate async task
function asyncTask() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(1);
    }, 1000);
  });
}

const asyncTasks = [asyncTask, asyncTask];

/**
 * createQueue returns enhanced promise
 */
const queue = createQueue(asyncTasks, {
  max: 1,
  interval: 1 * 1000,
  recordError: false,
});

queue.then((result) => {
  console.log(result);
});

const unsubscribe = queue.subscribe(({ taskIndex }) => {
  // queue will be paused after first task
  taskIndex === 0 && queue.pause();
});

setTimeout(() => {
  // queue paused after first task done, it will rerun the queue
  queue.resume();
}, 1500);

Lisence

Copyright (c) 2020 Jacano Licensed under the MIT license.

best-queue's People

Contributors

jcanno avatar dependabot[bot] avatar zoispag 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.