2016-08-17 45 views
-1

我需要幫助理解HTTP承諾鏈。返回並使用鏈接http承諾功能

我想有以下情形:

public class Serv1 extends IServ1 { 

    public HelpMe() : ng.IHttpPromise<any> { 

      $http.post(something) -> function(somethingResult) { 

       // I want to return this promise from this method and use it outside 
       $http.get(somethingResult) 
     } 
    } 
} 

在我的其他服務,我想使用Serv1.HelpMe方法:

public class Serv2 extends IServ2 { 

    public UseHelpMePromise() { 
     var scope = this; 
     this.serv1.HelpMe() -> function(resultOfInnerHelpMePromise){  
      scope.doLogic(resultOfInnerHelpMePromise) 
     } 
    } 
} 

我希望你能幫助我,告訴我哪些關鍵字我應該使用上面代碼中使用的' - >'。 我應該使用.then還是應該使用.success

此外,如果我想返回內部承諾(GET方法承諾),何時應該在HelpMe方法中放置返回語句?

最後我應該使用.catch/.error還是沒有一個?

回答

1

你應該做的是這樣的:

public class Serv1 extends IServ1 { 
    public HelpMe(): ng.IHttpPromise<any> { 
     return $http.post(something).then(somethingResult => { 
      return $http.get(somethingResult); 
     }); 
    } 
} 

public class Serv2 extends IServ2 { 
    public UseHelpMePromise() { 
     this.serv1.HelpMe().then(resultOfInnerHelpMePromise => { 
      this.doLogic(resultOfInnerHelpMePromise); 
     }); 
    } 
}