Capture async exceptions to promises using domains
Using domains it is possible to capture unhandled asynchronous exceptions. This module wraps that magic to promises.
var qdomain = require"qdomain";qdomainsetTimeoutthrow "async error";100;then// nothing...// We will get the thrown async error here!;
qdomain takes a callback and returns a promise. It can be resolved or
rejected using following methods:
- Throw an exception on this tick, the next or whenever to reject it. That's the magic of domains!
rejecton the given defer object
- It's full [Q defer object][defer]
- Return another promise
This makes it easy to capture stream errors while piping for example
qdomainfscreateReadStream"somefile"pipetransform1pipetransform2pipetransform3pipefscreateWriteStream"output-file"on"close" deferresolve;fail// Any IO errors or transform errors will be handled here;
When the promise is resolved or rejected the domain associated with it will be disposed automatically. Which means all IO and timers must be completed before resolving the promise or they will be canceled! This has few nice features:
- No more errors can be thrown from the
- You can be sure that all IO started from the callback has been completed (or canceled) when the promise is resolved or rejected.
npm install qdomain