-
Notifications
You must be signed in to change notification settings - Fork 0
/
domain.js
29 lines (26 loc) · 941 Bytes
/
domain.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
var EventEmitter = require('events').EventEmitter
var domain = require('domain')
var emitter1 = new EventEmitter()
var domain1 = domain.create()
domain1.on('error', function(error) {
console.log('domain1 处理这个错误 (' + error.message + ')')
})
// 显式绑定
domain1.add(emitter1)
emitter1.on('error', function(error) {
console.log('监听器处理此错误 (' + error.message + ')')
})
emitter1.emit('error', new Error('通过监听器来处理'))
emitter1.removeAllListeners('error')
emitter1.emit('error', new Error('通过 domain1 来处理'))
var domain2 = domain.create()
domain2.on('error', function(error) {
console.log('domain2 处理这个错误 (' + error.message + ')')
})
// 隐式绑定
domain2.run(function() {
var emitter2 = new EventEmitter()
emitter2.emit('error', new Error('通过 domain2 来处理'))
})
domain1.remove(emitter1)
emitter1.emit('error', new Error('转换为异常,系统将崩溃!'))