-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.js
36 lines (34 loc) · 976 Bytes
/
consumer.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
const { wait } = require('./example-utils');
async function consumer(type, producer, payload) {
if (type === 0) {
console.log('Consumer ready.');
return producer(1, consumer, 'Start sending!');
}
if (type === 1) {
const { acc, current } = payload;
await wait(1);
const orders = acc.concat(current);
console.log(`Consumer received: ${current}`);
if (current > 19) {
console.log('Stop sending...');
producer(2);
return orders;
} else if (current % 3 === 0) {
console.log('Consumer requests action...');
const action = (value) =>
new Promise((resolve) => {
console.log('Action: wait for 3s');
setTimeout(() => {
resolve(value);
}, 3000);
});
return producer(1, consumer, {
orders,
previous: current,
action
});
}
return producer(1, consumer, { orders, previous: current });
}
}
module.exports = consumer;