Skip to content

Promise.bind does not bind context for the entire promise chain when thisArg is a Promise #702

@seebees

Description

@seebees

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()
      })
  })
})

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions