All Projects → Jcanno → best-queue

Jcanno / best-queue

Licence: MIT license
Queue in runtime based promise

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to best-queue

ProtoPromise
Robust and efficient library for management of asynchronous operations in C#/.Net.
Stars: ✭ 20 (-23.08%)
Mutual labels:  task, promise, await
Task Easy
A simple, customizable, and lightweight priority queue for promises.
Stars: ✭ 244 (+838.46%)
Mutual labels:  task, queue, promise
relaks
Asynchrounous React component
Stars: ✭ 49 (+88.46%)
Mutual labels:  promise, await
Taskbuilder.fs
F# computation expression builder for System.Threading.Tasks
Stars: ✭ 217 (+734.62%)
Mutual labels:  task, await
leek
Celery Tasks Monitoring Tool
Stars: ✭ 77 (+196.15%)
Mutual labels:  task, queue
Ytask
YTask is an asynchronous task queue for handling distributed jobs in golang(go异步任务框架)
Stars: ✭ 121 (+365.38%)
Mutual labels:  task, queue
Unityfx.async
Asynchronous operations (promises) for Unity3d.
Stars: ✭ 143 (+450%)
Mutual labels:  task, promise
Asyncex
A helper library for async/await.
Stars: ✭ 2,794 (+10646.15%)
Mutual labels:  task, await
Then
🎬 Tame async code with battle-tested promises
Stars: ✭ 908 (+3392.31%)
Mutual labels:  task, promise
try-to-catch
functional try-catch wrapper for promises
Stars: ✭ 30 (+15.38%)
Mutual labels:  promise, await
csharp-workshop
NDC London 2019, Workshop: Become a better C# programmer: more Value, more Expressions, no Waiting
Stars: ✭ 21 (-19.23%)
Mutual labels:  task, await
WebsocketPromisify
Makes websocket's API just like REST with Promise-like API, with native Promises.
Stars: ✭ 18 (-30.77%)
Mutual labels:  promise, await
Swimmer
🏊 Swimmer - An async task pooling and throttling utility for JS
Stars: ✭ 94 (+261.54%)
Mutual labels:  task, await
Flowa
🔥Service level control flow for Node.js
Stars: ✭ 66 (+153.85%)
Mutual labels:  task, promise
Node Rethinkdb Job Queue
A persistent job or task queue backed by RethinkDB.
Stars: ✭ 158 (+507.69%)
Mutual labels:  task, queue
Fennel
A task queue library for Python and Redis
Stars: ✭ 24 (-7.69%)
Mutual labels:  task, queue
do
Simplest way to manage asynchronicity
Stars: ✭ 33 (+26.92%)
Mutual labels:  promise, await
Asyncawaitbestpractices
Extensions for System.Threading.Tasks.Task and System.Threading.Tasks.ValueTask
Stars: ✭ 693 (+2565.38%)
Mutual labels:  task, await
Inc.runtime.queue
An runtime queue use Asynchronous program
Stars: ✭ 19 (-26.92%)
Mutual labels:  task, queue
qless-php
PHP Bindings for qless
Stars: ✭ 25 (-3.85%)
Mutual labels:  task, queue


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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].