source: trip-planner-front/node_modules/fastq/test/example.ts@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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