qdomain - Promises from domains

  •        0

Small experiment combining node.js [domain][] error handling with promisesusing the [Q][] library.Using domains it is possible to capture unhandled asynchronous exceptions.This module wraps that magic to promises.```javascriptvar qdomain = require("qdomain");qdomain(function(defer){ setTimeout(function(){ throw new Error("async error"); }, 100);}).then(function(){ // nothing...}, function(err){ // We will get the thrown async error here!});````qdomain` takes a callback and returns a promise. It can be resolved orrejected using following methods: 1. Throw an exception on this tick, the next or whenever to reject it. That's the magic of domains! 2. Call `resolve` or `reject` on the given defer object - It's full [Q defer object][defer] 3. Return another promiseThis makes it easy to capture stream errors while piping for example```javascriptqdomain(function(defer){ fs.createReadStream("somefile") .pipe(transform1()) .pipe(transform2()) .pipe(transform3()) .pipe(fs.createWriteStream("output-file")) .on("close", defer.resolve);}).fail(function(err){ // Any IO errors or transform errors will be handled here});```




comments powered by Disqus

Related Projects