如何在没有外部缓存的情况下将"this"传递给Promise?

Mat*_*ood 10 javascript caching asynchronous this promise

我有一个名为LangDataService.isDataReadyPromise wawiting 的变量.一旦解决了一些逻辑就会发生.我怎么能把它传递给Promise?

LangDataService.isDataReady.then(function () {
    this.modalOn()
});
Run Code Online (Sandbox Code Playgroud)

我知道我可以缓存,var self_ = this;但我对其他选择感到好奇吗?

mat*_*141 21

LangDataService.isDataReady.then(function () {
  this.modalOn()
}.bind(this));
Run Code Online (Sandbox Code Playgroud)

  • @MatthewHarwood 可能你将 `bind` 调用链接到 `.then()`,而不是 `function () {...}`。 (4认同)
  • 伙计,我发誓我试过了,哈哈。我一定是把它放错了地方。 (2认同)