source: imaps-frontend/node_modules/fastq/test/example.ts

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.4 KB
RevLine 
[d565449]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.running()
36
37queue.saturated = () => undefined
38
39queue.unshift('world', (err, result) => {
40 if (err) throw err
41 console.log('the result is', result)
42})
43
44queue.unshift('unshift without cb')
45
46function worker(task: any, cb: fastq.done) {
47 cb(null, 'hello ' + task)
48}
49
50// Generics example
51
52interface GenericsContext {
53 base: number;
54}
55
56const genericsQueue = fastq<GenericsContext, number, string>({ base: 6 }, genericsWorker, 1)
57
58genericsQueue.push(7, (err, done) => {
59 if (err) throw err
60 console.log('the result is', done)
61})
62
63genericsQueue.unshift(7, (err, done) => {
64 if (err) throw err
65 console.log('the result is', done)
66})
67
68function genericsWorker(this: GenericsContext, task: number, cb: fastq.done<string>) {
69 cb(null, 'the meaning of life is ' + (this.base * task))
70}
71
72const queue2 = queueAsPromised(asyncWorker, 1)
73
74async function asyncWorker(task: any) {
75 return 'hello ' + task
76}
77
78async function run () {
79 await queue.push(42)
80 await queue.unshift(42)
81}
82
83run()
Note: See TracBrowser for help on using the repository browser.