Created
March 30, 2016 14:02
-
-
Save popthestack/40cfa560f1b3027ddb82c64ec0310b73 to your computer and use it in GitHub Desktop.
Passing methods that use `this` into promises or promisifying them with bluebird can lead to confusing results. This isn't specific to promises, but rather how the prototype works.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Promise = require('bluebird'); | |
function Blah() { | |
this.foo = 'blah'; | |
} | |
Blah.prototype.tester = function() { | |
console.log(this.foo); | |
}; | |
var blah = new Blah(); | |
blah.tester(); // 'blah' | |
var lala = Promise.promisify(blah.tester); | |
lala(); // undefined | |
var lala2 = Promise.promisify(blah.tester).bind(blah); | |
lala2(); // 'blah' | |
var test = new Promise(function(resolve, reject) { | |
return resolve('lol'); | |
}); | |
test.then(blah.tester); // undefined | |
test.then(blah.tester.bind(blah)); // blah |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment