简体   繁体   English

在Promise中处理上下文的正确方法

[英]Correct way of handling context in Promise

There are a few posts on the topic, but couldn't find one that explains the concept of context in Promises. 关于该主题的文章很少,但是找不到一个可以解释Promises中上下文概念的文章。 Lets start with some code (this is taken from an Ember.js module and simplified, but could be any JS code that supports promises): 让我们从一些代码开始(这是从Ember.js模块中提取并简化的,但是可以是任何支持promise的JS代码):

module.exports = CoreObject.extend({

init: function(pluginOptions, parentObject) {
//These are the properties that I want to access in methods below.
this.parentObject = parentObject;
this.propertyA = pluginOptions.propertyA;
this.propertyB = pluginOptions.propertyB;

},

startProcessing: function(whatToProcess) {
/* The following line does not work which is okay
      return this.prepareForProcessing(whatToProcess).then(process).then(postProcess(processedData, this); */

//This line does work, but parameters to then don't work. The result of prepareForProcessing is not passed to process and so on.
      return this.prepareForProcessing(whatToProcess).then(this.process).then(this.postProcess);

},

prepareForProcessing: function(whatToProcess) {
 //this does not work as 'this' is set to a different context
 //What does 'this' refer to here?
 //How do I access propertyA, propertyB defined at the beginning of this object?
  if(this.propertyA) {
  ....
}
process: function(preparedData) {
  //this does not work either
  if(this.propertyB) {
  .....
  }
}
postProces: function(processedData, options) {
 //This should work for obvious reasons but is the best way?
 if( options.propertyA) {
  ......
 }

}

}
})

Now, my questions are as follows: 现在,我的问题如下:

  1. Refer to comments inside prepareForProcessing function above. 请参阅上面prepareForProcessing函数内部的注释。 What does the 'this' variable refer to inside the method, when called from a 'then' method of a promise? 从promise的“ then”方法中调用时,“ this”变量在方法内部指的是什么? If I dump the 'this' object, it seems like it refers to some global node/ember cli object and not this module. 如果我转储“ this”对象,似乎它指向某个全局节点/ ember cli对象,而不是此模块。
  2. How do I retrieve/access the above mentioned properties in the method? 如何在方法中检索/访问上述属性? One obvious way would be to pass option as arguments, but not sure if that is the right way.If you look at the code here (line number 34), the options are being passed around for each 'then' call. 一种明显的方法是将option作为参数传递,但是不确定这是否正确。如果您查看此处的代码(第34行),则每次“ then”调用都会传递选项。 But then does this not go against OOP principles of having class/instance level variables that can be reused? 但这是否违反了具有可重用的类/实例级别变量的OOP原则? I'm relatively new to JS and don't understand the 'Object' based model, completely, so please forgive me if this sounded like a stupid question. 我是JS的新手,完全不了解基于“对象”的模型,因此,如果这听起来像一个愚蠢的问题,请原谅我。

I would appreciate any help & guidance. 我将不胜感激任何帮助和指导。 Thank you so very much. 非常感谢你。

您需要使用Function.prototype.bind

this.prepareForProcessing(whatToProcess).then(this.process.bind(this)).then(this.postProcess.bind(this));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM