othiym23 / node-continuation-local-storage

implementation of https://github.com/joyent/node/issues/5243
BSD 2-Clause "Simplified" License
1.13k stars 207 forks source link

How to deal with multiple Promise impls? #81

Open RobinQu opened 8 years ago

RobinQu commented 8 years ago

In a single project there may be

All these Promise impls will be mixed together in a Promise chain and contexts can be easily lost if any of these Promise implementations are not patched.

Any better solutions other than monkey-pactch all the Promise impl found in node_modules?

enicholson commented 8 years ago

Other than patching the world, I think you can make sure to adapt all promises your application receives from libraries out of your control (e.g. using Q(unknownPromise) or bluebird.resolve(unknownPromise)).

That should also guarantee the CLS context is tracked in the rest of the promise chain.

RobinQu commented 8 years ago

Sounds feasible, but your business code will be like:

const Promise = require('bluebird');
patchPromise(Promise);
const service = {
  foo: function() {
        return Promise.resolve(service1.bar().then(()=> service2.koo()));
  }
};

It doesn't look DRY at all.