Skip to content

Instantly share code, notes, and snippets.

@popthestack
Created March 30, 2016 14:02
Show Gist options
  • Save popthestack/40cfa560f1b3027ddb82c64ec0310b73 to your computer and use it in GitHub Desktop.
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.
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