Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for node js domains #113

Closed
mkeshavgarg opened this issue Oct 27, 2015 · 5 comments
Closed

Support for node js domains #113

mkeshavgarg opened this issue Oct 27, 2015 · 5 comments

Comments

@mkeshavgarg
Copy link

promise.js breaks node js domains.

//express.js
domain = require('domain');
app.use(function(req, res, next) {
    var reqDomain = domain.create();

    reqDomain.run( function (req, res, next) {
        console.log(process.domain);     //defined

        Promise.all([promise1, promise2]).then(function() {
            console.log(process.domain);    //  ERROR: process.domain is undefined
        }) 
    })
})
@ForbesLindesay
Copy link
Member

If you want to use domains you need to do require('promise/domains') instead of require('promise'). You shouldn't really need domains when using promises though, since promises handle error bubbling properly anyway.

@edef1c
Copy link
Member

edef1c commented Oct 28, 2015

It's also worth noting that domains are deprecated (https://nodejs.org/api/domain.html#domain_domain)

@ForbesLindesay
Copy link
Member

Yes, that too. I never understood why anyone thought they were a good idea. I guess it was just people who didn't understand promises.

@mkeshavgarg
Copy link
Author

I am not using domains for error bubbling.

I am aware that domains will soon be fully deprecated and will be replaced by some other API but for now my use case needs node.js domains.

In java there is a concept called thread local which helps to define variables in the context of that thread but in node we do not have anything like that but domains.
I found domains very useful in providing context for particular set of operations and mimicking thread local storage.

Use Case:

  1. I need to access the request context in mongoose model pre/post hooks
  2. I need to access the request context in request handlers

Solution: Create domain in the context of request

app.use(function(req, res, next) {
    var reqDomain = domain.create();
    reqDomain.run(next);
});

Now with the use of process.domain, I will be able to access 'data' bound to above created domain anywhere
Eg:

var d = process.domain
d.obj = {};

Similar use case has been discussed here:
nodejs/node-v0.x-archive#3733

@ForbesLindesay
Copy link
Member

You can easily implement this yourself on top of promises via something like:

var Promise = require('promise');
var originalThen = Promise.prototype.then;
Promise.prototype.then = function (cb, eb) {
  if (typeof cb === 'function') {
    // bind cb to the current domain
  }
  if (typeof eb === 'function') {
    // bind eb to the current domain
  }
  return originalThen.call(this, cb, eb);
};

We're not going to provide it by default because it represents a huge performance hit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants