{"id":22260891,"url":"https://github.com/jcanno/best-queue","last_synced_at":"2025-07-24T11:03:57.288Z","repository":{"id":42595909,"uuid":"247092359","full_name":"Jcanno/best-queue","owner":"Jcanno","description":"Queue in runtime based promise","archived":false,"fork":false,"pushed_at":"2024-09-27T07:21:41.000Z","size":1431,"stargazers_count":37,"open_issues_count":1,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T16:49:48.092Z","etag":null,"topics":["async","await","promise","queue","task"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Jcanno.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-13T14:38:08.000Z","updated_at":"2024-12-19T09:51:43.000Z","dependencies_parsed_at":"2023-02-08T17:01:26.630Z","dependency_job_id":"a7329d24-a61b-4796-bdb8-34b07ad81a8d","html_url":"https://github.com/Jcanno/best-queue","commit_stats":{"total_commits":151,"total_committers":5,"mean_commits":30.2,"dds":"0.16556291390728473","last_synced_commit":"e0256ff1e7346875c0f53754ccb6b115d98ffe59"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jcanno%2Fbest-queue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jcanno%2Fbest-queue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jcanno%2Fbest-queue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jcanno%2Fbest-queue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Jcanno","download_url":"https://codeload.github.com/Jcanno/best-queue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248686063,"owners_count":21145413,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","await","promise","queue","task"],"created_at":"2024-12-03T09:10:37.555Z","updated_at":"2025-04-13T08:40:36.503Z","avatar_url":"https://github.com/Jcanno.png","language":"TypeScript","readme":"\u003ch1 align=\"center\"\u003e\u003cimg height=\"300\" with=\"700\" src=\"https://raw.githubusercontent.com/Jcanno/images/master/best-queue/logo.png\" /\u003e\u003cbr\u003e \u003c/h1\u003e\n\n\u003ch4 align=\"center\"\u003e\n\n[![license](https://img.shields.io/badge/license-MIT-green.svg)](https://www.npmjs.org/package/best-queue)\n[![downloads](https://img.shields.io/npm/dt/best-queue)](https://www.npmjs.org/package/best-queue)\n[![size](https://img.shields.io/bundlephobia/min/best-queue/3.0.0)](https://www.npmjs.org/package/best-queue)\n[![issues](https://img.shields.io/github/issues-closed/Jcanno/best-queue)](https://www.npmjs.org/package/best-queue)\n[![npm](https://img.shields.io/npm/v/best-queue)](https://www.npmjs.org/package/best-queue)\n\n\u003c/h4\u003e\n\n\u003ch4 align=\"center\"\u003e\n\nEnglish|[简体中文](https://github.com/Jcanno/best-queue/blob/master/README.zh-CN.md)\n\n\u003c/h4\u003e\n\n\n## Introduction\n\n`best-queue` let you control tasks in a queue.\n\nIt's easy to execute task one by one with interval time in queue like this:\n\n```js\nQueue -\u003e task -\u003e wait(interval) -\u003e task -\u003e wait(interval) -\u003e task -\u003e finish\n```\n\nHow about adding async task in queue:\n\n```js\nQueue -\u003e Promise.resolve(task) -\u003e wait(interval) -\u003e Promise.resolve(task) -\u003e wait(interval) -\u003e Promise.resolve(task) -\u003e finish\n```\n\nWhat if we want to execute two tasks at the same time to support concurrency in queue:\n\n```js\nQueue -\u003e Promise.all([Promise.resolve(task), Promise.resolve(task)]) -\u003e wait(interval) -\u003e Promise.all([Promise.resolve(task), Promise.resolve(task)]) -\u003e wait(interval) -\u003e Promise.all([Promise.resolve(task), Promise.resolve(task)]) -\u003e finish\n```\n\nBut 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.\n\nThat's `best-queue` do. See image below.\n\n![](https://raw.githubusercontent.com/Jcanno/images/master/best-queue/queue-en.png)\n\n## Install\n\ntype in the command line to install with:\n\n```js\nnpm i best-queue\n```\n\n## Usage\n\nImport as an ES Module:\n\n```js\nimport { createQueue } from \"best-queue\";\n```\n\nRequire in Node:\n\n```js\nconst { createQueue } = require(\"best-queue\");\n```\n\nUse Script source(Exposed `BQueue` as global var):\n\n```js\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/best-queue\"\u003e\u003c/script\u003e\n\u003cscript src=\"https://unpkg.com/best-queue\"\u003e\u003c/script\u003e\n```\n\n## **API**\n\n| Attribute/Method                 | Description                                                    | Type                                                 | Default                                                  |\n| :------------------------------- | -------------------------------------------------------------- | :--------------------------------------------------- | -------------------------------------------------------- |\n| createQueue                      | create a queue                                                 | (tasks: unkonwn[], options: Options) =\u003e Promise\u003cany\u003e |                                                          |\n| options                          | create a queue by options                                      | Object                                               | {\u003cbr\u003emax: 1, \u003cbr\u003einterval: 0,\u003cbr\u003erecordError: false\u003cbr\u003e} |\n| options.max                      | max concurrence task at the same time, default and min to 1    | Number                                               | 1                                                        |\n| options.interval                 | the interval time between tow tasks(milliscond), default to 0  | Number                                               | 0                                                        |\n| options.recordError              | record error task instead of reject queue when task gone error | Boolean                                              | false                                                    |\n| pause()                          | `pause` the queue, queue stop to execute task                  | Function(): void                                     |\n| resume()                         | rerun the queue                                                | Function(): void                                     |\n| subscribe(listener: Listener)  | listener fired a task done                           | Function(({taskStatus: 'success' \\| 'error', data: unknown, taskIndex: number, progress: number}) =\u003e void): () =\u003e void                                         |\n\n## Example\n\n```js\nimport { createQueue } from \"best-queue\";\n\n// simulate async task\nfunction asyncTask() {\n  return new Promise((resolve) =\u003e {\n    setTimeout(() =\u003e {\n      resolve(1);\n    }, 1000);\n  });\n}\n\nconst asyncTasks = [asyncTask, asyncTask];\n\n/**\n * createQueue returns enhanced promise\n */\nconst queue = createQueue(asyncTasks, {\n  max: 1,\n  interval: 1 * 1000,\n  recordError: false,\n});\n\nqueue.then((result) =\u003e {\n  console.log(result);\n});\n\nconst unsubscribe = queue.subscribe(({ taskIndex }) =\u003e {\n  // queue will be paused after first task\n  taskIndex === 0 \u0026\u0026 queue.pause();\n});\n\nsetTimeout(() =\u003e {\n  // queue paused after first task done, it will rerun the queue\n  queue.resume();\n}, 1500);\n```\n\n## Lisence\n\nCopyright (c) 2020 Jacano Licensed under the MIT license.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcanno%2Fbest-queue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjcanno%2Fbest-queue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcanno%2Fbest-queue/lists"}