source: imaps-frontend/node_modules/fastq/README.md@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

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