node-continuation-local-storage icon indicating copy to clipboard operation
node-continuation-local-storage copied to clipboard

How to deal with multiple Promise impls?

Open RobinQu opened this issue 8 years ago • 2 comments

In a single project there may be

  • multiple versions of Promise implementation, say bluebird@2, bluebird@1
  • multiple packages of Promise implementation, say coexistence of bluebird and Q.

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?

RobinQu avatar Aug 15 '16 06:08 RobinQu

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.

enicholson avatar Aug 15 '16 11:08 enicholson

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.

RobinQu avatar Aug 20 '16 04:08 RobinQu