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.bind` does not bind context for the entire promise chain when `thisArg` is a Promise #702

Closed
seebees opened this issue Jul 14, 2015 · 2 comments

Comments

@seebees
Copy link

@seebees seebees commented Jul 14, 2015

Perhaps I do not understand what bind is suppose to do? I would expect both of these tests to pass.

node: 0.12.7
bluebird: 2.9.33

var expect = require('chai').expect;
var Promise = require('bluebird');

describe('Derived promises should have the bound context', function () {

  it('when the context is an object', function() {
    return Promise
      .bind({hasContext: true})
      .then(function(){
        expect(this.hasContext).to.be.true()
      })
      .then(function(){
        expect(this.hasContext).to.be.true()
      })
      .then(function(){
        expect(this.hasContext).to.be.true()
      })
      .then(function(){
        expect(this.hasContext).to.be.true()
      })
      .then(function(){
        expect(this.hasContext).to.be.true()
      })
  })

  // This fails :(
  it('when the context is a Promise', function() {
    return Promise
      .bind(new Promise(function(resolve){
        setTimeout(function(){
          resolve({hasContext: true})
        }, 100)
      }))
      .then(function(){
        // this will pass
        expect(this.hasContext).to.be.true()
      })
      .then(function(){
         // these all fail
        expect(this.hasContext).to.be.true()
      })
      .then(function(){
        expect(this.hasContext).to.be.true()
      })
      .then(function(){
        expect(this.hasContext).to.be.true()
      })
      .then(function(){
        expect(this.hasContext).to.be.true()
      })
  })
})
@petkaantonov
Copy link
Owner

@petkaantonov petkaantonov commented Jul 15, 2015

In the latter case it's the test that's broken.

You have resolve({getId: function(){return 1}}) while it should be resolve({hasContext: true})

@seebees
Copy link
Author

@seebees seebees commented Jul 15, 2015

Thanks! Especially for looking at the test again. I'm sorry I did not catch that. I edited it for posterity.

I'm sure you have heard it before, but great library, and thanks for all the time you put into maintaining it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.