| 1 | # fastq
|
|---|
| 2 |
|
|---|
| 3 | ![ci][ci-url]
|
|---|
| 4 | [![npm version][npm-badge]][npm-url]
|
|---|
| 5 |
|
|---|
| 6 | Fast, in memory work queue.
|
|---|
| 7 |
|
|---|
| 8 | Benchmarks (1 million tasks):
|
|---|
| 9 |
|
|---|
| 10 | * setImmediate: 812ms
|
|---|
| 11 | * fastq: 854ms
|
|---|
| 12 | * async.queue: 1298ms
|
|---|
| 13 | * neoAsync.queue: 1249ms
|
|---|
| 14 |
|
|---|
| 15 | Obtained on node 12.16.1, on a dedicated server.
|
|---|
| 16 |
|
|---|
| 17 | If you need zero-overhead series function call, check out
|
|---|
| 18 | [fastseries](http://npm.im/fastseries). For zero-overhead parallel
|
|---|
| 19 | function call, check out [fastparallel](http://npm.im/fastparallel).
|
|---|
| 20 |
|
|---|
| 21 | * <a href="#install">Installation</a>
|
|---|
| 22 | * <a href="#usage">Usage</a>
|
|---|
| 23 | * <a href="#api">API</a>
|
|---|
| 24 | * <a href="#license">Licence & copyright</a>
|
|---|
| 25 |
|
|---|
| 26 | ## Install
|
|---|
| 27 |
|
|---|
| 28 | `npm i fastq --save`
|
|---|
| 29 |
|
|---|
| 30 | ## Usage (callback API)
|
|---|
| 31 |
|
|---|
| 32 | ```js
|
|---|
| 33 | 'use strict'
|
|---|
| 34 |
|
|---|
| 35 | const queue = require('fastq')(worker, 1)
|
|---|
| 36 |
|
|---|
| 37 | queue.push(42, function (err, result) {
|
|---|
| 38 | if (err) { throw err }
|
|---|
| 39 | console.log('the result is', result)
|
|---|
| 40 | })
|
|---|
| 41 |
|
|---|
| 42 | function worker (arg, cb) {
|
|---|
| 43 | cb(null, arg * 2)
|
|---|
| 44 | }
|
|---|
| 45 | ```
|
|---|
| 46 |
|
|---|
| 47 | ## Usage (promise API)
|
|---|
| 48 |
|
|---|
| 49 | ```js
|
|---|
| 50 | const queue = require('fastq').promise(worker, 1)
|
|---|
| 51 |
|
|---|
| 52 | async function worker (arg) {
|
|---|
| 53 | return arg * 2
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | async function run () {
|
|---|
| 57 | const result = await queue.push(42)
|
|---|
| 58 | console.log('the result is', result)
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | run()
|
|---|
| 62 | ```
|
|---|
| 63 |
|
|---|
| 64 | ### Setting "this"
|
|---|
| 65 |
|
|---|
| 66 | ```js
|
|---|
| 67 | 'use strict'
|
|---|
| 68 |
|
|---|
| 69 | const that = { hello: 'world' }
|
|---|
| 70 | const queue = require('fastq')(that, worker, 1)
|
|---|
| 71 |
|
|---|
| 72 | queue.push(42, function (err, result) {
|
|---|
| 73 | if (err) { throw err }
|
|---|
| 74 | console.log(this)
|
|---|
| 75 | console.log('the result is', result)
|
|---|
| 76 | })
|
|---|
| 77 |
|
|---|
| 78 | function worker (arg, cb) {
|
|---|
| 79 | console.log(this)
|
|---|
| 80 | cb(null, arg * 2)
|
|---|
| 81 | }
|
|---|
| 82 | ```
|
|---|
| 83 |
|
|---|
| 84 | ### Using with TypeScript (callback API)
|
|---|
| 85 |
|
|---|
| 86 | ```ts
|
|---|
| 87 | 'use strict'
|
|---|
| 88 |
|
|---|
| 89 | import * as fastq from "fastq";
|
|---|
| 90 | import type { queue, done } from "fastq";
|
|---|
| 91 |
|
|---|
| 92 | type Task = {
|
|---|
| 93 | id: number
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | const q: queue<Task> = fastq(worker, 1)
|
|---|
| 97 |
|
|---|
| 98 | q.push({ id: 42})
|
|---|
| 99 |
|
|---|
| 100 | function worker (arg: Task, cb: done) {
|
|---|
| 101 | console.log(arg.id)
|
|---|
| 102 | cb(null)
|
|---|
| 103 | }
|
|---|
| 104 | ```
|
|---|
| 105 |
|
|---|
| 106 | ### Using with TypeScript (promise API)
|
|---|
| 107 |
|
|---|
| 108 | ```ts
|
|---|
| 109 | 'use strict'
|
|---|
| 110 |
|
|---|
| 111 | import * as fastq from "fastq";
|
|---|
| 112 | import type { queueAsPromised } from "fastq";
|
|---|
| 113 |
|
|---|
| 114 | type Task = {
|
|---|
| 115 | id: number
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | const q: queueAsPromised<Task> = fastq.promise(asyncWorker, 1)
|
|---|
| 119 |
|
|---|
| 120 | q.push({ id: 42}).catch((err) => console.error(err))
|
|---|
| 121 |
|
|---|
| 122 | async function asyncWorker (arg: Task): Promise<void> {
|
|---|
| 123 | // No need for a try-catch block, fastq handles errors automatically
|
|---|
| 124 | console.log(arg.id)
|
|---|
| 125 | }
|
|---|
| 126 | ```
|
|---|
| 127 |
|
|---|
| 128 | ## API
|
|---|
| 129 |
|
|---|
| 130 | * <a href="#fastqueue"><code>fastqueue()</code></a>
|
|---|
| 131 | * <a href="#push"><code>queue#<b>push()</b></code></a>
|
|---|
| 132 | * <a href="#unshift"><code>queue#<b>unshift()</b></code></a>
|
|---|
| 133 | * <a href="#pause"><code>queue#<b>pause()</b></code></a>
|
|---|
| 134 | * <a href="#resume"><code>queue#<b>resume()</b></code></a>
|
|---|
| 135 | * <a href="#idle"><code>queue#<b>idle()</b></code></a>
|
|---|
| 136 | * <a href="#length"><code>queue#<b>length()</b></code></a>
|
|---|
| 137 | * <a href="#getQueue"><code>queue#<b>getQueue()</b></code></a>
|
|---|
| 138 | * <a href="#kill"><code>queue#<b>kill()</b></code></a>
|
|---|
| 139 | * <a href="#killAndDrain"><code>queue#<b>killAndDrain()</b></code></a>
|
|---|
| 140 | * <a href="#error"><code>queue#<b>error()</b></code></a>
|
|---|
| 141 | * <a href="#concurrency"><code>queue#<b>concurrency</b></code></a>
|
|---|
| 142 | * <a href="#drain"><code>queue#<b>drain</b></code></a>
|
|---|
| 143 | * <a href="#empty"><code>queue#<b>empty</b></code></a>
|
|---|
| 144 | * <a href="#saturated"><code>queue#<b>saturated</b></code></a>
|
|---|
| 145 | * <a href="#promise"><code>fastqueue.promise()</code></a>
|
|---|
| 146 |
|
|---|
| 147 | -------------------------------------------------------
|
|---|
| 148 | <a name="fastqueue"></a>
|
|---|
| 149 | ### fastqueue([that], worker, concurrency)
|
|---|
| 150 |
|
|---|
| 151 | Creates a new queue.
|
|---|
| 152 |
|
|---|
| 153 | Arguments:
|
|---|
| 154 |
|
|---|
| 155 | * `that`, optional context of the `worker` function.
|
|---|
| 156 | * `worker`, worker function, it would be called with `that` as `this`,
|
|---|
| 157 | if that is specified.
|
|---|
| 158 | * `concurrency`, number of concurrent tasks that could be executed in
|
|---|
| 159 | parallel.
|
|---|
| 160 |
|
|---|
| 161 | -------------------------------------------------------
|
|---|
| 162 | <a name="push"></a>
|
|---|
| 163 | ### queue.push(task, done)
|
|---|
| 164 |
|
|---|
| 165 | Add a task at the end of the queue. `done(err, result)` will be called
|
|---|
| 166 | when the task was processed.
|
|---|
| 167 |
|
|---|
| 168 | -------------------------------------------------------
|
|---|
| 169 | <a name="unshift"></a>
|
|---|
| 170 | ### queue.unshift(task, done)
|
|---|
| 171 |
|
|---|
| 172 | Add a task at the beginning of the queue. `done(err, result)` will be called
|
|---|
| 173 | when the task was processed.
|
|---|
| 174 |
|
|---|
| 175 | -------------------------------------------------------
|
|---|
| 176 | <a name="pause"></a>
|
|---|
| 177 | ### queue.pause()
|
|---|
| 178 |
|
|---|
| 179 | Pause the processing of tasks. Currently worked tasks are not
|
|---|
| 180 | stopped.
|
|---|
| 181 |
|
|---|
| 182 | -------------------------------------------------------
|
|---|
| 183 | <a name="resume"></a>
|
|---|
| 184 | ### queue.resume()
|
|---|
| 185 |
|
|---|
| 186 | Resume the processing of tasks.
|
|---|
| 187 |
|
|---|
| 188 | -------------------------------------------------------
|
|---|
| 189 | <a name="idle"></a>
|
|---|
| 190 | ### queue.idle()
|
|---|
| 191 |
|
|---|
| 192 | Returns `false` if there are tasks being processed or waiting to be processed.
|
|---|
| 193 | `true` otherwise.
|
|---|
| 194 |
|
|---|
| 195 | -------------------------------------------------------
|
|---|
| 196 | <a name="length"></a>
|
|---|
| 197 | ### queue.length()
|
|---|
| 198 |
|
|---|
| 199 | Returns the number of tasks waiting to be processed (in the queue).
|
|---|
| 200 |
|
|---|
| 201 | -------------------------------------------------------
|
|---|
| 202 | <a name="getQueue"></a>
|
|---|
| 203 | ### queue.getQueue()
|
|---|
| 204 |
|
|---|
| 205 | Returns all the tasks be processed (in the queue). Returns empty array when there are no tasks
|
|---|
| 206 |
|
|---|
| 207 | -------------------------------------------------------
|
|---|
| 208 | <a name="kill"></a>
|
|---|
| 209 | ### queue.kill()
|
|---|
| 210 |
|
|---|
| 211 | Removes all tasks waiting to be processed, and reset `drain` to an empty
|
|---|
| 212 | function.
|
|---|
| 213 |
|
|---|
| 214 | -------------------------------------------------------
|
|---|
| 215 | <a name="killAndDrain"></a>
|
|---|
| 216 | ### queue.killAndDrain()
|
|---|
| 217 |
|
|---|
| 218 | Same than `kill` but the `drain` function will be called before reset to empty.
|
|---|
| 219 |
|
|---|
| 220 | -------------------------------------------------------
|
|---|
| 221 | <a name="error"></a>
|
|---|
| 222 | ### queue.error(handler)
|
|---|
| 223 |
|
|---|
| 224 | Set a global error handler. `handler(err, task)` will be called
|
|---|
| 225 | each time a task is completed, `err` will be not null if the task has thrown an error.
|
|---|
| 226 |
|
|---|
| 227 | -------------------------------------------------------
|
|---|
| 228 | <a name="concurrency"></a>
|
|---|
| 229 | ### queue.concurrency
|
|---|
| 230 |
|
|---|
| 231 | Property that returns the number of concurrent tasks that could be executed in
|
|---|
| 232 | parallel. It can be altered at runtime.
|
|---|
| 233 |
|
|---|
| 234 | -------------------------------------------------------
|
|---|
| 235 | <a name="paused"></a>
|
|---|
| 236 | ### queue.paused
|
|---|
| 237 |
|
|---|
| 238 | Property (Read-Only) that returns `true` when the queue is in a paused state.
|
|---|
| 239 |
|
|---|
| 240 | -------------------------------------------------------
|
|---|
| 241 | <a name="drain"></a>
|
|---|
| 242 | ### queue.drain
|
|---|
| 243 |
|
|---|
| 244 | Function that will be called when the last
|
|---|
| 245 | item from the queue has been processed by a worker.
|
|---|
| 246 | It can be altered at runtime.
|
|---|
| 247 |
|
|---|
| 248 | -------------------------------------------------------
|
|---|
| 249 | <a name="empty"></a>
|
|---|
| 250 | ### queue.empty
|
|---|
| 251 |
|
|---|
| 252 | Function that will be called when the last
|
|---|
| 253 | item from the queue has been assigned to a worker.
|
|---|
| 254 | It can be altered at runtime.
|
|---|
| 255 |
|
|---|
| 256 | -------------------------------------------------------
|
|---|
| 257 | <a name="saturated"></a>
|
|---|
| 258 | ### queue.saturated
|
|---|
| 259 |
|
|---|
| 260 | Function that will be called when the queue hits the concurrency
|
|---|
| 261 | limit.
|
|---|
| 262 | It can be altered at runtime.
|
|---|
| 263 |
|
|---|
| 264 | -------------------------------------------------------
|
|---|
| 265 | <a name="promise"></a>
|
|---|
| 266 | ### fastqueue.promise([that], worker(arg), concurrency)
|
|---|
| 267 |
|
|---|
| 268 | Creates a new queue with `Promise` apis. It also offers all the methods
|
|---|
| 269 | and properties of the object returned by [`fastqueue`](#fastqueue) with the modified
|
|---|
| 270 | [`push`](#pushPromise) and [`unshift`](#unshiftPromise) methods.
|
|---|
| 271 |
|
|---|
| 272 | Node v10+ is required to use the promisified version.
|
|---|
| 273 |
|
|---|
| 274 | Arguments:
|
|---|
| 275 | * `that`, optional context of the `worker` function.
|
|---|
| 276 | * `worker`, worker function, it would be called with `that` as `this`,
|
|---|
| 277 | if that is specified. It MUST return a `Promise`.
|
|---|
| 278 | * `concurrency`, number of concurrent tasks that could be executed in
|
|---|
| 279 | parallel.
|
|---|
| 280 |
|
|---|
| 281 | <a name="pushPromise"></a>
|
|---|
| 282 | #### queue.push(task) => Promise
|
|---|
| 283 |
|
|---|
| 284 | Add a task at the end of the queue. The returned `Promise` will be fulfilled (rejected)
|
|---|
| 285 | when the task is completed successfully (unsuccessfully).
|
|---|
| 286 |
|
|---|
| 287 | This promise could be ignored as it will not lead to a `'unhandledRejection'`.
|
|---|
| 288 |
|
|---|
| 289 | <a name="unshiftPromise"></a>
|
|---|
| 290 | #### queue.unshift(task) => Promise
|
|---|
| 291 |
|
|---|
| 292 | Add a task at the beginning of the queue. The returned `Promise` will be fulfilled (rejected)
|
|---|
| 293 | when the task is completed successfully (unsuccessfully).
|
|---|
| 294 |
|
|---|
| 295 | This promise could be ignored as it will not lead to a `'unhandledRejection'`.
|
|---|
| 296 |
|
|---|
| 297 | <a name="drained"></a>
|
|---|
| 298 | #### queue.drained() => Promise
|
|---|
| 299 |
|
|---|
| 300 | Wait for the queue to be drained. The returned `Promise` will be resolved when all tasks in the queue have been processed by a worker.
|
|---|
| 301 |
|
|---|
| 302 | This promise could be ignored as it will not lead to a `'unhandledRejection'`.
|
|---|
| 303 |
|
|---|
| 304 | ## License
|
|---|
| 305 |
|
|---|
| 306 | ISC
|
|---|
| 307 |
|
|---|
| 308 | [ci-url]: https://github.com/mcollina/fastq/workflows/ci/badge.svg
|
|---|
| 309 | [npm-badge]: https://badge.fury.io/js/fastq.svg
|
|---|
| 310 | [npm-url]: https://badge.fury.io/js/fastq
|
|---|