Within a javascript class function, I'm unable to access outside variables (and therefore this
) within a then scope.
I've referred to this article as well as it's duplicates Object method with ES6 / Bluebird promises
I'm aware that the following is a valid solution for accessing the desired this
inside of a then scope:
var a = this;
one().then(function () {
console.log(a)
});
However, I can't seem to get it working this way inside the context of a class. Here is the class:
var outerestThis = 'foo'
export class Listener {
start() {
this.listenForSearch();
}
listenForSearch() {
var outererThis = 'foo';
const search = () => {
var outerThis = 'foo';
let artistName = $('#search-input').val();
new ArtistSearch(artistName).fetch_and_render().then(function () {
debugger;
this.starListener();
});
}
$('#search-button').click(function () {
search();
});
});
}
starListener() {
...
}
}
When I hit the debugger inside of const search() inside the then scope, I expect that I should be able to access outer varables (which I would change to outer this
when I get things working). But, outerThis, outererThis and outerestThis are all undefined.