项目原始demo,不改动
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
To repozytorium jest zarchiwizowane. Możesz wyświetlać pliki i je sklonować, ale nie możesz do niego przepychać zmian lub otwierać zgłoszeń/Pull Requestów.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # run-queue
  2. A promise based, dynamic priority queue runner, with concurrency limiting.
  3. ```js
  4. const RunQueue = require('run-queue')
  5. const queue = new RunQueue({
  6. maxConcurrency: 1
  7. })
  8. queue.add(1, example, [-1])
  9. for (let ii = 0; ii < 5; ++ii) {
  10. queue.add(0, example, [ii])
  11. }
  12. const finished = []
  13. queue.run().then(
  14. console.log(finished)
  15. })
  16. function example (num, next) {
  17. setTimeout(() => {
  18. finished.push(num)
  19. next()
  20. }, 5 - Math.abs(num))
  21. }
  22. ```
  23. would output
  24. ```
  25. [ 0, 1, 2, 3, 4, -1 ]
  26. ```
  27. If you bump concurrency to `2`, then you get:
  28. ```
  29. [ 1, 0, 3, 2, 4, -1 ]
  30. ```
  31. The concurrency means that they don't finish in order, because some take
  32. longer than others. Each priority level must finish entirely before the
  33. next priority level is run. See
  34. [PRIORITIES](https://github.com/iarna/run-queue#priorities) below. This is
  35. even true if concurrency is set high enough that all of the regular queue
  36. can execute at once, for instance, with `maxConcurrency: 10`:
  37. ```
  38. [ 4, 3, 2, 1, 0, -1 ]
  39. ```
  40. ## API
  41. ### const queue = new RunQueue(options)
  42. Create a new queue. Options may contain:
  43. * maxConcurrency - (Default: `1`) The maximum number of jobs to execute at once.
  44. * Promise - (Default: global.Promise) The promise implementation to use.
  45. ### queue.add (prio, fn, args)
  46. Add a new job to the end of the queue at priority `prio` that will run `fn`
  47. with `args`. If `fn` is async then it should return a Promise.
  48. ### queue.run ()
  49. Start running the job queue. Returns a Promise that resolves when either
  50. all the jobs are complete or a job ends in error (throws or returns a
  51. rejected promise). If a job ended in error then this Promise will be rejected
  52. with that error and no further queue running will be done.
  53. ## PRIORITIES
  54. Priorities are any integer value >= 0.
  55. Lowest is executed first.
  56. Priorities essentially represent distinct job queues. All jobs in a queue
  57. must complete before the next highest priority job queue is executed.
  58. This means that if you have two queues, `0` and `1` then ALL jobs in `0`
  59. must complete before ANY execute in `1`. If you add new `0` level jobs
  60. while `1` level jobs are running then it will switch back processing the `0`
  61. queue and won't execute any more `1` jobs till all of the new `0` jobs
  62. complete.