I'm trying to understand node.js domains as they apply to nested callbacks.
var d = domain.create().on("error", function(err) {
//handle error
}).enter();
doAsync1(function (err) {
doAsync2(function (err) {
doAsync3(function (err) {
doAsync4(function (err) {
})
})
});
d.exit();
Where does the domain's error coverage start and finish here? I assume it only covers doAsync1
(but not the callback passed into doAsync1
) because otherwise what is the point of d.bind
(and d.intercept
)?
Also, does it cover ALL of what doAsync1
does, ie including all the async things it does before returning?
var d = domain.create().on("error", function(err) {
//handle error
}).enter();
doAsync1(d.intercept(function () { // intercept the callback
doAsync2(function (err) {
doAsync3(function (err) {
doAsync4(function (err) {
})
})
}));
d.exit();
Again, where does the domain's error coverage start and finish? I assume it covers doAsync1
and doAsync2
(because doAsync2
is executed in the intercepted callback to doAsync1
). But does intercept
mean that doAsync3
and doAsync4
are also covered? If so, do we need to pass err
into doAsync2
, doAsync3
and doAsync4
's callbacks, or do those also get intercepted?
If doAsync3
and doAsync4
ARE also covered by the intercept, is there any way of covering only doAsync1
and doAsync2
?
Further, what is the best way of only covering, say doAsync1
and doAsync3
with domains (perhaps they're untrusted 3rd party functions), but leaving doAsync2
and doAsync4
exposed to application crashes (because those are functions we wrote)?
References to promises, EventEmitter, generator, etc patterns not required.