【问题标题】:return and use chained http promise from a function从函数返回并使用链式 http 承诺
【发布时间】:2016-12-24 14:33:15
【问题描述】:

我在理解 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

另外,如果我想返回内部promise(GET方法promise),我应该什么时候在HelpMe方法中加入return语句?

最后我应该使用.catch / .error 还是不使用它们?

【问题讨论】:

    标签: javascript angularjs typescript promise


    【解决方案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);
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-16
      • 2017-04-05
      • 1970-01-01
      • 2014-05-16
      • 2020-07-30
      • 2018-09-12
      • 2015-03-14
      • 1970-01-01
      相关资源
      最近更新 更多