-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
async.js
62 lines (55 loc) 路 1.23 KB
/
async.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict'
const Hemera = require('./../../packages/hemera')
const parallel = require('async/parallel')
const compose = require('async/compose')
const nats = require('nats').connect()
const hemera = new Hemera(nats, {
logLevel: 'info'
})
hemera.ready(() => {
hemera.add(
{
topic: 'math',
cmd: 'add'
},
(req, cb) => {
cb(null, req.a + req.b)
}
)
/**
* Control flow
* Execute multiple acts in parallel
*/
parallel(
[
callback => {
hemera.act('topic:math,cmd:add,a:1,b:2', callback)
},
callback => {
hemera.act('topic:math,cmd:add,a:2,b:2', callback)
},
callback => {
hemera.act('topic:math,cmd:add,a:3,b:2', callback)
}
],
function(err, results) {
console.log(err, results)
}
)
/**
* Composition
*/
const add1 = (n, callback) => {
hemera.act({ topic: 'math', cmd: 'add', a: n, b: 1 }, callback)
}
const add2 = (n, callback) => {
hemera.act({ topic: 'math', cmd: 'add', a: n, b: 2 }, callback)
}
const add1and2 = compose(add1, add2)
add1and2(10, function(err, result) {
console.log(err, result)
})
/**
* For more examples see http://caolan.github.io/async/docs.html
*/
})