0.0.3 • Published 12 years ago

qdomain v0.0.3

Weekly downloads
1
License
-
Repository
github
Last release
12 years ago

qdomain - Promises from domains

Small experiment combining node.js domain error handling with promises using the q library.

Using domains it is possible to capture unhandled asynchronous exceptions. This module wraps that magic to promises.

var 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. That callback can be resolved or rejected using following methods:

  1. Throw an exception on this tick, the next or whatever. That's the magic of domains!
  2. Call resolve or reject on the given defer object
  3. Return another promise

This makes it easy to capture stream errors while piping for example

qdomain(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
});

Install

npm install qdomain
0.0.3

12 years ago

0.0.2

12 years ago

0.0.1

12 years ago