source: frontend/node_modules/fastq/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 8.0 KB
RevLine 
[9af201e]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 * <a href="#install">Installation</a>
22 * <a href="#usage">Usage</a>
23 * <a href="#api">API</a>
24 * <a href="#license">Licence &amp; copyright</a>
25
26## Install
27
28`npm i fastq --save`
29
30## Usage (callback API)
31
32```js
33'use strict'
34
35const queue = require('fastq')(worker, 1)
36
37queue.push(42, function (err, result) {
38 if (err) { throw err }
39 console.log('the result is', result)
40})
41
42function worker (arg, cb) {
43 cb(null, arg * 2)
44}
45```
46
47## Usage (promise API)
48
49```js
50const queue = require('fastq').promise(worker, 1)
51
52async function worker (arg) {
53 return arg * 2
54}
55
56async function run () {
57 const result = await queue.push(42)
58 console.log('the result is', result)
59}
60
61run()
62```
63
64### Setting "this"
65
66```js
67'use strict'
68
69const that = { hello: 'world' }
70const queue = require('fastq')(that, worker, 1)
71
72queue.push(42, function (err, result) {
73 if (err) { throw err }
74 console.log(this)
75 console.log('the result is', result)
76})
77
78function 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
89import * as fastq from "fastq";
90import type { queue, done } from "fastq";
91
92type Task = {
93 id: number
94}
95
96const q: queue<Task> = fastq(worker, 1)
97
98q.push({ id: 42})
99
100function 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
111import * as fastq from "fastq";
112import type { queueAsPromised } from "fastq";
113
114type Task = {
115 id: number
116}
117
118const q: queueAsPromised<Task> = fastq.promise(asyncWorker, 1)
119
120q.push({ id: 42}).catch((err) => console.error(err))
121
122async 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
151Creates a new queue.
152
153Arguments:
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
165Add a task at the end of the queue. `done(err, result)` will be called
166when the task was processed.
167
168-------------------------------------------------------
169<a name="unshift"></a>
170### queue.unshift(task, done)
171
172Add a task at the beginning of the queue. `done(err, result)` will be called
173when the task was processed.
174
175-------------------------------------------------------
176<a name="pause"></a>
177### queue.pause()
178
179Pause the processing of tasks. Currently worked tasks are not
180stopped.
181
182-------------------------------------------------------
183<a name="resume"></a>
184### queue.resume()
185
186Resume the processing of tasks.
187
188-------------------------------------------------------
189<a name="idle"></a>
190### queue.idle()
191
192Returns `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
199Returns the number of tasks waiting to be processed (in the queue).
200
201-------------------------------------------------------
202<a name="getQueue"></a>
203### queue.getQueue()
204
205Returns 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
211Removes all tasks waiting to be processed, and reset `drain` to an empty
212function.
213
214-------------------------------------------------------
215<a name="killAndDrain"></a>
216### queue.killAndDrain()
217
218Same 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
224Set a global error handler. `handler(err, task)` will be called
225each 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
231Property that returns the number of concurrent tasks that could be executed in
232parallel. It can be altered at runtime.
233
234-------------------------------------------------------
235<a name="paused"></a>
236### queue.paused
237
238Property (Read-Only) that returns `true` when the queue is in a paused state.
239
240-------------------------------------------------------
241<a name="drain"></a>
242### queue.drain
243
244Function that will be called when the last
245item from the queue has been processed by a worker.
246It can be altered at runtime.
247
248-------------------------------------------------------
249<a name="empty"></a>
250### queue.empty
251
252Function that will be called when the last
253item from the queue has been assigned to a worker.
254It can be altered at runtime.
255
256-------------------------------------------------------
257<a name="saturated"></a>
258### queue.saturated
259
260Function that will be called when the queue hits the concurrency
261limit.
262It can be altered at runtime.
263
264-------------------------------------------------------
265<a name="promise"></a>
266### fastqueue.promise([that], worker(arg), concurrency)
267
268Creates a new queue with `Promise` apis. It also offers all the methods
269and properties of the object returned by [`fastqueue`](#fastqueue) with the modified
270[`push`](#pushPromise) and [`unshift`](#unshiftPromise) methods.
271
272Node v10+ is required to use the promisified version.
273
274Arguments:
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
284Add a task at the end of the queue. The returned `Promise` will be fulfilled (rejected)
285when the task is completed successfully (unsuccessfully).
286
287This promise could be ignored as it will not lead to a `'unhandledRejection'`.
288
289<a name="unshiftPromise"></a>
290#### queue.unshift(task) => Promise
291
292Add a task at the beginning of the queue. The returned `Promise` will be fulfilled (rejected)
293when the task is completed successfully (unsuccessfully).
294
295This promise could be ignored as it will not lead to a `'unhandledRejection'`.
296
297<a name="drained"></a>
298#### queue.drained() => Promise
299
300Wait 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
302This promise could be ignored as it will not lead to a `'unhandledRejection'`.
303
304## License
305
306ISC
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
Note: See TracBrowser for help on using the repository browser.