然后链接的Java脚本失去了这个的上下文

时间:2016-10-26 05:27:46

标签: javascript promise

我在Java脚本中使用.then链接混乱了。试图弄清楚无法解决以下问题。

我想要达到的目标是: 我有两个数组this.arrayA和this.arrayB和functionA和funcitonB分别填充它们然后我想在funcitonC中使用这两个数组。

return functionA(this)  //returns promise resolve , populates this.arrayA
.then(functionB(this))  //returns promise resolve , populates this.arrayB
.then(functionC(this))  //returns promise rejected if condition=true else resolves promise 

我试图通过如下指定传递“this”上下文,但我总是丢失引用,在functionB中我总是得到functionA的输出。我已经尝试了下面的代码,但它不起作用。

var self = this;
 return functionA(this)  //returns promise resolve , populates this.arrayA
.then(functionB.bind(self))  //returns promise resolve , populates this.arrayB
.then(functionC.bind(self))  //returns promise rejected if condition=true else resolves promise 

var _ = require('loadash');
functionA(that){
   return new Promise(function(resolve, reject) {
     resolve(
     _.each(that.items1, function(item1){
          var someStruct = {
              name: item1.name,
              type: item1.type
          }
          that.arrayA.push(someStruct);
        }       
     )       
   });
}

functionB(that){
   return new Promise(function(resolve, reject) {
     resolve(
     _.each(that.items2, function(item2){
          var someStruct = {
              name: item2.name,
              type: item2.type
          }
          that.arrayB.push(someStruct);
        }       
     )       
   });
}

functionC(that){
   return new Promise(function(resolve, reject){         
     _.each(that.arrayA, function(someItems){
         _.find(that.arrayB , function(someMoreItems){
               if (someItem.name === someMoreItems.name && someItem.type != someMoreItems.type){
                 reject(Error("it's broken"))
               }else{
                 resolve()
               }
         });             
     });
   });
 }

你能教育我这样我能解决这个问题吗?

感谢

3 个答案:

答案 0 :(得分:0)

将上下文this存储到变量中,并且每次都使用该变量。

答案 1 :(得分:0)

尝试将函数而不是对象发送到Promise构造函数。

所以,而不是

new Promise({})

你需要

new Promise(function(){})

更新 - 猜猜2:

您正在绑定函数的外部thisthis,但这些函数从未使用过。您想要绑定第一个参数,而不是this

尝试更改

functionX.bind(self)

functionX.bind(null, this)

答案 2 :(得分:0)

上述代码的另一个问题是如何在functionC中解析和拒绝Promise。因为一旦你为Promise设置了一个状态,它就不会在functionC的情况下改变,一对迭代已经设置了解决的承诺,并且满足if语句的条件不能再设置为reject()。所以我设置了一个布尔标志并评估了两个循环之外的标志。而且我需要return承诺状态只是设置它不会返回它。

 return functionA(this)  //returns promise resolve , populates this.arrayA
.then(functionB.bind(this))  //returns promise resolve , populates this.arrayB
.then(functionC.bind(this))  //returns promise rejected if condition=true  else resolves promise 


functionA(that){
    self.arrayB = [];
    return new Promise(function(resolve, reject) { 
    _.each(that.items1, function(item1){
      var someStruct = {
          name: item1.name,
          type: item1.type
      }
      resolve(that.arrayA.push(someStruct));
    }       

  });
 }

functionB(){
 var self = this;
 self.arrayB = [];
 return new Promise(function(resolve, reject) {     
 _.each(self.items2, function(item2){
      var someStruct = {
          name: item2.name,
          type: item2.type
      }
      resolve(self.arrayB.push(someStruct));
    }            
 });
}

functionC(){
  var self = this;
  var promiseStatus = false;
  return new Promise(function(resolve, reject){         
 _.each(self.arrayA, function(someItems){
     _.find(self.arrayB , function(someMoreItems){
           if (someItem.name === someMoreItems.name && someItem.type != someMoreItems.type){
              promiseStatus = true;              
           }
       });           
 });
 if (promiseStatus){return reject(Error('it broken'))}
 else {return resolve();}
 });
 }