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.reduce() broken in bluebird v2 #12

Closed
overlookmotel opened this issue Jun 25, 2016 · 2 comments
Closed

Promise.reduce() broken in bluebird v2 #12

overlookmotel opened this issue Jun 25, 2016 · 2 comments

Comments

@overlookmotel
Copy link
Collaborator

Promise.reduce() loses CLS context with bluebird v2.

Example:

var cls = require('continuation-local-storage');
var ns = cls.createNamespace('test');

var Promise = require('bluebird');
require('cls-bluebird')(ns);

ns.run(function() {
    ns.set('id', 1);
    Promise.reduce([1, 2, 3], function(total, v) {
        console.log('In context 1:', ns.get('id'));
    }, 0);
});

ns.run(function() {
    ns.set('id', 2);
    Promise.reduce([1, 2, 3], function(total, v) {
        console.log('In context 2:', ns.get('id'));
    }, 0);
});

Expected output is:

In context 1: 1
In context 1: 1
In context 1: 1
In context 2: 2
In context 2: 2
In context 2: 2

But in reality you get:

In context 1: 1
In context 1: 1
In context 1: 1
In context 2: 1
In context 2: 1
In context 2: 1

So CLS context is getting mixed up between the two reduce callbacks.

@overlookmotel
Copy link
Collaborator Author

Here's the fix:

shimmer.wrap(Promise, 'reduce', function(reduce) {
    return function(arr, fn, value) {
        return reduce.call(this, arr, ns.bind(fn), value);
    };
});

shimmer.wrap(Promise.prototype, 'reduce', function(reduce) {
    return function(fn, value) {
        return reduce.call(this, ns.bind(fn), value);
    };
});

@overlookmotel
Copy link
Collaborator Author

Fixed in v2.0.0

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

1 participant