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.

81 lines
1.4 KiB

2 years ago
  1. import * as fastq from '../'
  2. import { promise as queueAsPromised } from '../'
  3. // Basic example
  4. const queue = fastq(worker, 1)
  5. queue.push('world', (err, result) => {
  6. if (err) throw err
  7. console.log('the result is', result)
  8. })
  9. queue.push('push without cb')
  10. queue.concurrency
  11. queue.drain()
  12. queue.empty = () => undefined
  13. console.log('the queue tasks are', queue.getQueue())
  14. queue.idle()
  15. queue.kill()
  16. queue.killAndDrain()
  17. queue.length
  18. queue.pause()
  19. queue.resume()
  20. queue.saturated = () => undefined
  21. queue.unshift('world', (err, result) => {
  22. if (err) throw err
  23. console.log('the result is', result)
  24. })
  25. queue.unshift('unshift without cb')
  26. function worker(task: any, cb: fastq.done) {
  27. cb(null, 'hello ' + task)
  28. }
  29. // Generics example
  30. interface GenericsContext {
  31. base: number;
  32. }
  33. const genericsQueue = fastq<GenericsContext, number, string>({ base: 6 }, genericsWorker, 1)
  34. genericsQueue.push(7, (err, done) => {
  35. if (err) throw err
  36. console.log('the result is', done)
  37. })
  38. genericsQueue.unshift(7, (err, done) => {
  39. if (err) throw err
  40. console.log('the result is', done)
  41. })
  42. function genericsWorker(this: GenericsContext, task: number, cb: fastq.done<string>) {
  43. cb(null, 'the meaning of life is ' + (this.base * task))
  44. }
  45. const queue2 = queueAsPromised(asyncWorker, 1)
  46. async function asyncWorker(task: any) {
  47. return 'hello ' + task
  48. }
  49. async function run () {
  50. await queue.push(42)
  51. await queue.unshift(42)
  52. }
  53. run()