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

Request context with promises? #17

Closed
SlowShip opened this issue Dec 19, 2016 · 5 comments
Closed

Request context with promises? #17

SlowShip opened this issue Dec 19, 2016 · 5 comments

Comments

@SlowShip
Copy link

Hey, great module. It's been really helpful.

I can't seem to get it to work with promises though, are they supported?

I've got a minimal replication of it failing here incase I'm doing anything wrong.

here is the main point of failure - the contextService.get works fine normally but returns undefined inside a promise:

app.get('/', (req, res) => {
  contextService.set('myNamespace:someVal', 'myValue');
  console.log(contextService.get('myNamespace:someVal')); // logs myValue as expected

  return Promise
  .resolve(null)
  .then(() => {
    console.log(contextService.get('myNamespace:someVal')); // logs undefined
    res.send('done...');
  });
});

Thanks for your time

@michaelkrone
Copy link
Owner

michaelkrone commented Dec 19, 2016

Hi @SlowShip,

glad this code is helpful.
Unfortunately this node.js bug prevents domains working with native Promises: nodejs/node-v0.x-archive#8648
As a workaround you might use bluebird as const Promise = require('bluebird'), or even global.Promise = ...

Since Domains are deprecated, I am working on an API compatible replacement.
Stay tuned. I will leave this bug open for tracking further process on this task.

@SlowShip
Copy link
Author

Cool. Thanks for the work arounds, will keep an eye on this.

@SlowShip
Copy link
Author

SlowShip commented Dec 20, 2016

For anyone else suffering the same issue, I ended up installing bluebird and using global.Promise = require('bluebird'); right at the top of my applications startup script as @michaelkrone suggested and it works beautifully. Just be careful as you're changing every promise use to another implementation which should be fine, but might have some side effects.

Thanks again.

@pieterwillaert
Copy link

any updates on this issue? this bug should be documented in the readme.

@cmpaul
Copy link

cmpaul commented Jul 16, 2018

This appears to be resolved (I'm using native promises on node.js v8.4.0 and express v4.16.3).

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

4 participants