Skip to main content
Removed wrong use of the word "context"
Source Link
trincot
  • 357.8k
  • 38
  • 282
  • 341

When you pass self.bar to the then method, you pass a function reference. Although it looks like you also specify it should be called on the self object, that is actually not what is happening. The context self object is not included in that function reference. The context (determining what this is)object's value is determined when a function is called, not when it is defined or passed as argument.

In your second example, self is the this object within the function context, because that is where you call the function from.

Another way to get it working is to force the function's contextthis object to always be self, overriding the above described behaviour. You can achieve that with .bind():

Promise.all([p1, p2])
    .then(self.bar.bind(self));

When you pass self.bar to the then method, you pass a function reference. Although it looks like you also specify it should be called on the self object, that is actually not what is happening. The context self is not in that function reference. The context (determining what this is) is determined when a function is called, not when it is defined or passed as argument.

In your second example, self is the context, because that is where you call the function from.

Another way to get it working is to force the function's context to always be self, overriding the above described behaviour. You can achieve that with .bind():

Promise.all([p1, p2])
    .then(self.bar.bind(self));

When you pass self.bar to the then method, you pass a function reference. Although it looks like you also specify it should be called on the self object, that is actually not what is happening. The self object is not included in that function reference. The this object's value is determined when a function is called, not when it is defined or passed as argument.

In your second example, self is the this object within the function context, because that is where you call the function from.

Another way to get it working is to force the function's this object to always be self, overriding the above described behaviour. You can achieve that with .bind():

Promise.all([p1, p2])
    .then(self.bar.bind(self));
added 95 characters in body
Source Link
trincot
  • 357.8k
  • 38
  • 282
  • 341

When you pass self.bar to the then method, you pass a function reference. Although it looks like you also specify it should be called on the self object, that is actually not what is happening. The context self is not in that function reference. The context (determining what this is) is determined when a function is called, not when it is defined or passed as argument.

In your second example, self is the context, because that is where you call the function from.

Another way to get it working is to force the function's context to always be self, overriding the above described behaviour. You can achieve that with .bind().bind():

Promise.all([p1, p2])
    .then(self.bar.bind(self));

When you pass self.bar to the then method, you pass a function reference. Although it looks like you also specify it should be called on the self object, that is actually not what is happening. The context self is not in that function reference. The context (determining what this is) is determined when a function is called, not when it is defined or passed as argument.

In your second example, self is the context, because that is where you call the function from.

Another way to get it working is to force the function's context to always be self, overriding the above described behaviour. You can achieve that with .bind():

Promise.all([p1, p2])
    .then(self.bar.bind(self));

When you pass self.bar to the then method, you pass a function reference. Although it looks like you also specify it should be called on the self object, that is actually not what is happening. The context self is not in that function reference. The context (determining what this is) is determined when a function is called, not when it is defined or passed as argument.

In your second example, self is the context, because that is where you call the function from.

Another way to get it working is to force the function's context to always be self, overriding the above described behaviour. You can achieve that with .bind():

Promise.all([p1, p2])
    .then(self.bar.bind(self));
deleted 104 characters in body
Source Link
castletheperson
  • 33.7k
  • 11
  • 75
  • 111

When you pass self.bar to the then method, you pass a function reference. Although it looks like you also specify it should be called on the self object, that is actually not what is happening. The context self is not in that function reference. The context (determining what this is) is determined when a function is called, not when it is defined or passed as argument.

In your second example, self is the context, because that is where you call the function from.

Another way to get it working is to force the function's context to always be self, overriding the above described behaviour. You can achieve that with .bind().bind():

Promise.all([p1, p2])
    .then(self.bar.bind(self));
});

When you pass self.bar to the then method, you pass a function reference. Although it looks like you also specify it should be called on the self object, that is actually not what is happening. The context self is not in that function reference. The context (determining what this is) is determined when a function is called, not when it is defined or passed as argument.

In your second example, self is the context, because that is where you call the function from.

Another way to get it working is to force the function's context to always be self, overriding the above described behaviour. You can achieve that with .bind():

Promise.all([p1, p2])
    .then(self.bar.bind(self));
});

When you pass self.bar to the then method, you pass a function reference. Although it looks like you also specify it should be called on the self object, that is actually not what is happening. The context self is not in that function reference. The context (determining what this is) is determined when a function is called, not when it is defined or passed as argument.

In your second example, self is the context, because that is where you call the function from.

Another way to get it working is to force the function's context to always be self, overriding the above described behaviour. You can achieve that with .bind():

Promise.all([p1, p2])
    .then(self.bar.bind(self));
Source Link
trincot
  • 357.8k
  • 38
  • 282
  • 341
Loading