Skip to content

Promise.all context propagation #59

Open
@jbellenger

Description

@jbellenger

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions