Consider the following code:
foo: function() {
var self = this;
var p1 = p2 = someFunctionThatReturnsAPromise();
Promise.all([p1, p2])
.then(self.bar);
}
bar: function(promises) {
var self = this;
console.log(self);
}
Output:
undefined
But if I do the following instead:
foo: function() {
var self = this;
var p1 = p2 = someFunctionThatReturnsAPromise();
Promise.all([p1, p2])
.then(function(result) {
self.bar(result);
});
}
bar: function(promises) {
var self = this;
console.log(self);
}
Output:
{ foo: [Function],
bar: [Function] }
I don't understand why the first call changes where this points in the bar function. Can someone enlighten me?