-
Notifications
You must be signed in to change notification settings - Fork 0
/
6_co_errors.js
47 lines (37 loc) · 924 Bytes
/
6_co_errors.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
// SAMPLE 6: Co Error handling
var co = require('co');
var subGenerator = function * () {
throw new Error('Subgenerator error');
};
var errorThrower = function(msg) {
return function(done) {
setImmediate(function() {
done(msg);
});
};
};
co(function *() {
try {
throw new Error('something odd happend');
} catch (e) {
console.log( e );
}
try {
yield subGenerator;
} catch (e) {
console.log( e );
}
try {
yield errorThrower('yet another strange thing happend');
} catch (e) {
console.log( e );
}
throw new Error('another odd thing happend');
})(function( err ) {
// uncought exceptions will end up here.
console.log( err );
});
// => [Error: something odd happend]
// => [Error: Subgenerator error]
// => yet another strange thing happend
// => [Error: another odd thing happend]