Debian Codegraph @

fastq/test/example.ts

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