Object method with ES6 / Bluebird promises
Why
this
ends up beingundefined
in this case?
Because you're passing a function, not a method-bound-to-an-instance. This problem is not even promise-specific, see How to access the correct `this` context inside a callback? for the generic solution:
….then(this.method2.bind(this))… // ES5 .bind() Function method
….then((r) => this.method2(r))… // ES6 arrow function
However, Bluebird does offer an other way to call the function as a method:
this.service.serviceMethod(args)
.bind(this)
.then(this.method2)
.catch(onRejected);
Calling object methods with promises and the 'this' context
No, you can use the bind
method as well:
Promise.resolve().then(test.sayFoo.bind(test));
See also How to access the correct `this` context inside a callback? for the generic problem.
However, Bluebird does offer an extra way to call the method:
Promise.resolve().bind(test).then(test.sayFoo);
How to use ES6 arrow functions with promise binding (bluebird)
If you don't want lexically bound this
, there's no need to use arrow functions. If you want dynamically bound this
, just don't use arrow functions.
Of course, you could scrap that .bind({})
as a whole and use arrow functions that are bound to the object by putting everything in an object method (or an IIFE in your example):
(function() {
this; // the value that everything is bound to
double(2).then(result => {
this.double = result; // store result here
return triple(2);
}).then(result => {
console.log('double:', this.double);
console.log('triple:', result);
console.log('sum:', this.double + result);
}).catch(err => {
console.log(err.message);
});
}.call({}));
However, there are much better ways to access previous results in a promise chain than contextual state, especially if you are using ES6!
Bluebird overwriting constructor
This is a regular Javascript problem with passing a function reference (nothing specific to do with promises).
When you pass this.myself
as a function argument, all that gets actually passed is a reference to the myself
function and the binding to the this
object is lost so when the function is called, the this
pointer will be wrong and won't point to your object's instance data. There are numerous ways to work around this. This is a generic Javascript issue with passing callback functions that are methods. There are a number of ways to work around this.
You can use .bind()
to keep the this
reference property attached like this:
return this.wait().then(this.myself.bind(this));
Related Topics
Why Is Console.Log() Considered Better Than Alert()
How to Loop Through All Dom Elements on a Page
Immediate Function Invocation Syntax
Disable Submit Button on Form Submit
JavaScript Variables Declare Outside or Inside Loop
Are There Pointers in JavaScript
How to Run Two Jquery Animations Simultaneously
Jquery Checkbox Checked State Changed Event
How to Check If Iframe Is Loaded or It Has a Content
Chrome Extension - Sendresponse Not Waiting for Async Function
How to Overload Functions in JavaScript
Merging or Combining Two Onedit Trigger Functions
Make Browser Window Blink in Task Bar
Why Does a String Index in an Array Not Increase the 'Length'
How to Strip All Punctuation from a String in JavaScript Using Regex