租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
8.0 KiB

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