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

Context lost when awaiting on custom thenable #37

Open
vkarpov15 opened this Issue Dec 11, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@vkarpov15
Copy link

vkarpov15 commented Dec 11, 2018

Not sure if this is an issue or expected behavior, but figured I'd ask. I've been tracking down a bug with mongoose and express-http-context, and found that if you use await with a custom then() function, context goes away. Here's an example:

const clsHooked = require('cls-hooked');

const session = clsHooked.createNamespace('mytest');

session.run(() => {
  run().catch(err => console.log(err));
});

async function run() {
  session.set('test', '42');

  const thenable = {
    then: (resolve, reject) => {
      console.log('Session', session.get('test')); // undefined
      return Promise.resolve('').then(resolve, reject);
    }
  };

  await thenable; // This is the problem, context gets lost in `then()`

  // This works fine
  await new Promise((resolve, reject) => {
    console.log('Session2', session.get('test')); // 42
  });
}

Any ideas as to what might be going on here?

Re: Automattic/mongoose#7292

@Jeff-Lewis

This comment has been minimized.

Copy link
Owner

Jeff-Lewis commented Jan 30, 2019

@vkarpov15 ! Sorry I haven't gotten to this. I'm a little fuzzy on what mongoose's exec() does under the hood but my first thought is that a custom then() would need to be wrapped similar to how cls-bluebird does it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.