basant307/AI_Governance_Project
048
1# fastq2 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: 812ms11* fastq: 854ms12* async.queue: 1298ms13* neoAsync.queue: 1249ms14 15Obtained on node 12.16.1, on a dedicated server.16 17If you need zero-overhead series function call, check out18[fastseries](http://npm.im/fastseries). For zero-overhead parallel19function call, check out [fastparallel](http://npm.im/fastparallel).20 21[](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 & copyright</a>27 28## Install29 30`npm i fastq --save`31 32## Usage (callback API)33 34```js35'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```js52const queue = require('fastq').promise(worker, 1)53 54async function worker (arg) {55 return arg * 256}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```js69'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```ts89'use strict'90 91import * as fastq from "fastq";92import type { queue, done } from "fastq";93 94type Task = {95 id: number96}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```ts111'use strict'112 113import * as fastq from "fastq";114import type { queueAsPromised } from "fastq";115 116type Task = {117 id: number118}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 automatically126 console.log(arg.id)127}128```129 130## API131 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 in161 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 called168when 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 called175when the task was processed.176 177-------------------------------------------------------178<a name="pause"></a>179### queue.pause()180 181Pause the processing of tasks. Currently worked tasks are not182stopped.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 tasks208 209-------------------------------------------------------210<a name="kill"></a>211### queue.kill()212 213Removes all tasks waiting to be processed, and reset `drain` to an empty214function.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 called227each 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.concurrency232 233Property that returns the number of concurrent tasks that could be executed in234parallel. It can be altered at runtime.235 236-------------------------------------------------------237<a name="paused"></a>238### queue.paused239 240Property (Read-Only) that returns `true` when the queue is in a paused state.241 242-------------------------------------------------------243<a name="drain"></a>244### queue.drain245 246Function that will be called when the last247item from the queue has been processed by a worker.248It can be altered at runtime.249 250-------------------------------------------------------251<a name="empty"></a>252### queue.empty253 254Function that will be called when the last255item from the queue has been assigned to a worker.256It can be altered at runtime.257 258-------------------------------------------------------259<a name="saturated"></a>260### queue.saturated261 262Function that will be called when the queue hits the concurrency263limit.264It can be altered at runtime.265 266-------------------------------------------------------267<a name="promise"></a>268### fastqueue.promise([that], worker(arg), concurrency)269 270Creates a new queue with `Promise` apis. It also offers all the methods271and properties of the object returned by [`fastqueue`](#fastqueue) with the modified272[`push`](#pushPromise) and [`unshift`](#unshiftPromise) methods.273 274Node v10+ is required to use the promisified version.275 276Arguments:277* `that`, optional context of the `worker` function.278* `worker`, worker function, it would be called with `that` as `this`,279 if that is specified. It MUST return a `Promise`.280* `concurrency`, number of concurrent tasks that could be executed in281 parallel.282 283<a name="pushPromise"></a>284#### queue.push(task) => Promise285 286Add a task at the end of the queue. The returned `Promise` will be fulfilled (rejected)287when the task is completed successfully (unsuccessfully).288 289This promise could be ignored as it will not lead to a `'unhandledRejection'`.290 291<a name="unshiftPromise"></a>292#### queue.unshift(task) => Promise293 294Add a task at the beginning of the queue. The returned `Promise` will be fulfilled (rejected)295when the task is completed successfully (unsuccessfully).296 297This promise could be ignored as it will not lead to a `'unhandledRejection'`.298 299<a name="drained"></a>300#### queue.drained() => Promise301 302Wait for the queue to be drained. The returned `Promise` will be resolved when all tasks in the queue have been processed by a worker.303 304This promise could be ignored as it will not lead to a `'unhandledRejection'`.305 306## License307 308ISC309 310[ci-url]: https://github.com/mcollina/fastq/workflows/ci/badge.svg311[npm-badge]: https://badge.fury.io/js/fastq.svg312[npm-url]: https://badge.fury.io/js/fastq313 