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.

example.ts 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.running()
  21. queue.saturated = () => undefined
  22. queue.unshift('world', (err, result) => {
  23. if (err) throw err
  24. console.log('the result is', result)
  25. })
  26. queue.unshift('unshift without cb')
  27. function worker(task: any, cb: fastq.done) {
  28. cb(null, 'hello ' + task)
  29. }
  30. // Generics example
  31. interface GenericsContext {
  32. base: number;
  33. }
  34. const genericsQueue = fastq<GenericsContext, number, string>({ base: 6 }, genericsWorker, 1)
  35. genericsQueue.push(7, (err, done) => {
  36. if (err) throw err
  37. console.log('the result is', done)
  38. })
  39. genericsQueue.unshift(7, (err, done) => {
  40. if (err) throw err
  41. console.log('the result is', done)
  42. })
  43. function genericsWorker(this: GenericsContext, task: number, cb: fastq.done<string>) {
  44. cb(null, 'the meaning of life is ' + (this.base * task))
  45. }
  46. const queue2 = queueAsPromised(asyncWorker, 1)
  47. async function asyncWorker(task: any) {
  48. return 'hello ' + task
  49. }
  50. async function run () {
  51. await queue.push(42)
  52. await queue.unshift(42)
  53. }
  54. run()