15

I have a variable called LangDataService.isDataReady that is a Promise wawiting to be resolved. Upon resolve some logic will happen. How can I pass this into that Promise?

LangDataService.isDataReady.then(function () {
    this.modalOn()
});

I know i can cache var self_ = this; but I'm curious of other alternatives?

Armeen Moon
  • 18,061
  • 35
  • 120
  • 233
  • 1
    [Arrow function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) – Andreas Jan 15 '16 at 20:23
  • 1
    I believe @Andreas is referring to the fact that `arrow functions` have a lexical-scoped `this`. – John Hascall Jan 15 '16 at 20:25
  • @Andreas unfortunatlly arrow functions aren't widely supported and babel will just transpile to var this_ = this; – Armeen Moon Jan 15 '16 at 20:26
  • 2
    You've asked for alternatives and this is one ;) [IE11 (and below), Safari and Konquerer](https://kangax.github.io/compat-table/es6/) are the only browser not supporting arrow functions. Chrome, Firefox and IE Edge do. Therefor I would disagree with the "widely" :) – Andreas Jan 15 '16 at 20:31

1 Answers1

33
LangDataService.isDataReady.then(function () {
  this.modalOn()
}.bind(this));
matt3141
  • 4,303
  • 1
  • 19
  • 24