-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
57 lines (46 loc) · 1.43 KB
/
test.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
const chai = require('chai')
const expect = chai.expect
const hello = new require('stream').Writable({
autoDestroy: true,
destroy (err, callback) {
console.log('6 before callback in _destroy')
process.nextTick(() => console.log('i. before _destory callback nextTick'))
callback(err)
console.log('7 after callback in _destory')
process.nextTick(() => console.log('iii. after _destory callback nextTick'))
},
final (callback) {
console.log('4 before callback in _final')
callback()
console.log('8 after callback in _final')
}
}).on('error', err => console.log('error'))
.on('finish', () => console.log('5 finish'))
.on('close', () => console.log('ii. close'))
console.log('1 before end')
process.nextTick(() => console.log('3 before end nextTicked'))
hello.end()
console.log('2 after end')
process.nextTick(() => console.log('9 after end nextTicked'))
/**
* ```
* $ node test.js
* 1 before end
* 2 after end
* 3 before end nextTicked
* 4 before callback in _final
* 5 finish
* 6 before callback in _destroy
* 7 after callback in _destory
* 8 after callback in _final
* 9 after end nextTicked
* i. before _destory callback nextTick
* ii. close
* iii. after _destory callback nextTick
* ```
*
* 1. _final is invoked in end() via nextTick
* 2. finish is emitted in _final's callback
* 3. _destroy is invoked in _final's callback
* 4. close is emitted in callback of _destroy via nextTick
*/