-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
generators.js
88 lines (77 loc) 路 1.49 KB
/
generators.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict'
const Hemera = require('./../../packages/hemera')
const nats = require('nats').connect()
const hemera = new Hemera(nats, {
logLevel: 'info'
})
hemera.ready(() => {
// You can also mix but this is not clean code
hemera.add(
{
topic: 'math',
cmd: 'divide'
},
function(req, reply) {
reply(null, {
result: req.a / req.b
})
}
)
hemera.ext('onServerPreRequest', function(ctx, req, res, next) {
next()
})
hemera.ext('onServerPreRequest', function*(ctx, req, res) {
return yield Promise.resolve()
})
hemera.add(
{
topic: 'math',
cmd: 'sub'
},
function*(req) {
var result = yield Promise.resolve({
result: req.a - req.b
})
return result
}
)
hemera.act('topic:math,cmd:divide,a:30,b:5', function(err, result) {
this.log.info(result)
})
hemera.add(
{
topic: 'math',
cmd: 'add'
},
function*(req) {
const add = yield Promise.resolve({
result: req.a + req.b
})
const sub = yield this.act('topic:math,cmd:sub,a:30,b:5')
return {
result: sub.result + add.result
}
}
)
hemera
.act({
topic: 'math',
cmd: 'add',
a: 10,
b: 20
})
.then(x => console.log(x))
hemera
.act(
{
topic: 'math',
cmd: 'add',
a: 10,
b: 10
},
function*(err, result) {
return yield result
}
)
.then(x => console.log(x))
})