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

Promise.all context propagation #59

Open
jbellenger opened this issue Jun 24, 2016 · 1 comment
Open

Promise.all context propagation #59

jbellenger opened this issue Jun 24, 2016 · 1 comment

Comments

@jbellenger
Copy link

jbellenger commented Jun 24, 2016

Async-listener will propagate contexts through Promise.all, which is different behavior from similar functionality in the domain module.

It's not 100% clear to me that this is a bug, though it is unexpected.

Test case:

'use strict';

// CLS
(() => {
  const cls = require('continuation-local-storage');
  const ns = cls.createNamespace('ns');
  const promises = [];

  for (let i=0; i < 3; i++) {
    const p = new Promise((resolve) => {
      ns.run(() => {
        ns.active.i = i;
        resolve(i)
      });
    });

    promises.push(p);
  }

  Promise.all(promises)
    .then(() => {
      console.log('[CLS] Promise.all active ns', ns.active);
    });
})();

// domain
(() => {
  const domain = require('domain');
  const d = domain.createDomain();
  const promises = [];

  for (let i=0; i < 3; i++) {
    const p = new Promise((resolve) => {
      d.run(() => {
        domain.active.i = i;
        resolve(i);
      });
    });

    promises.push(p);
  };

  Promise.all(promises)
    .then(() => {
      console.log('[Domain] Promise.all active domain', d.active);
    });
})();

Output:

$ node -version
v6.2.2

$ node cls-test.js
[CLS] Promise.all active ns { i: 2 }
[Domain] Promise.all active domain undefined
@hayes
Copy link
Collaborator

hayes commented Jul 11, 2016

This is by design, although there is a lot of debate as too the correct approach. There is currently an open issue on CLS discussig how promises should propagate contexts. The approach asl took was to propagate from resolution to callback rather than creation to callback. For example, if a promise wraps an api that makes a network request, the callback passed to then would be a child of that request rather than a sibling. This allows consumers of the asl api to trace causality. In the Promise.all case, the thing resolving the promise is the same thing as the last promise to resolve that was passed to Promise.all.

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

2 participants