-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-chain.js
More file actions
34 lines (25 loc) · 975 Bytes
/
Copy pathtest-chain.js
File metadata and controls
34 lines (25 loc) · 975 Bytes
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
const singletonAsync = require('./singleton-async.js');
const EventEmitter = require('events');
const myEmitter = new EventEmitter();
const asyncAction = (a) => new Promise((resolve, reject) => {
setTimeout(() => { console.log('asyncAction returns:', a);
reject(a);
}, 1000);
})
const getAsyncData = (a) => asyncAction(a)
.then((aa) => console.log('getAsyncData returns', aa))
.catch(() => console.log('error'))
// const getAsyncData = (a) => asyncAction(a)
let chain = Promise.resolve();
myEmitter.on('event', (e) => {
chain = chain.then(() => getAsyncData(e))
});
myEmitter.emit('event', 'a');
myEmitter.emit('event', 'b');
myEmitter.emit('event', 'c');
myEmitter.emit('event', 'a1');
myEmitter.emit('event', 'b1');
myEmitter.emit('event', 'c1');
setTimeout(() => myEmitter.emit('event', 'd'), 1001)
setTimeout(() => myEmitter.emit('event', 'e'), 2000)
setTimeout(() => myEmitter.emit('event', 'f'), 3000)