AngularJS:为什么。然后()不等待承诺的对象到达 [英] AngularJS: Why is .then() not waiting for the promise object to arrive

查看:138
本文介绍了AngularJS:为什么。然后()不等待承诺的对象到达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了下面的角度服务使API调用的目的。

I have created the following angular service for the purpose of making the API calls.

(function (module) {
    module.service('APIService',function ($http, $q) {
        console.log("Reached APIService")
        var deferred = $q.defer();
        this.getData = function (requestURL) {
            console.log("Reached APIService @GET", requestURL);
            $http.get(requestURL).success(
                function (data, status) {
                    deferred.resolve(data);
                    console.log("In service",status,data);
                }).error(
                function (data, status) {
                    deferred.reject(data);
                    console.log(status);
                });
            return deferred.promise;
        };

        this.postData = function (requestURL, requestObj) {
            console.log("Reached APIService @POST", requestURL, requestObj);
            $http.post(requestURL, requestObj).success(
                function (data, status) {
                    deferred.resolve(data);
                    console.log(status);
                }).error(
                function (data, status) {
                    deferred.reject(data);
                    console.log(status);
                });
            return deferred.promise;
        };
    });
}(angular.module("MainApp")));

我在我的两个控制器注入它。不过,我现在面临以下几个问题:

I have injected it in my two controllers. However, I am facing following issues:


  1. 当我把它称为第一次在第一个控制器,它工作正常,并返回所需的结果。然而,当我把它排在第二控制器如下:

  1. When I call it first time in first controller, it works fine and returns the desired result. However, when I call it in second controller as follows:

    APIService.getData(Config + headerURL).then(function (response) {
        console.log(Config + headerURL);
        console.log("header response from API", response);

    },function(error) {
        console.log("API call for header data failed");
    });


由于我的服务回报承诺的对象,我不希望里面。然后在code()的业务数据到达之前工作。
 然而,它运行的服务的数据到达之前(不知道如何)。最奇怪的是,我进去。那么()的响应实际上并没有从这个URL(配置+ headerURL)的反应,但它是从不同的URL previously称为第一控制器获得的响应使用相同APIService服务

Since my service returns a promise object, I don't expect the code inside .then() to work before the service data arrives. However, it runs before service data arrives (no clue how). The most strange thing is that the response that I get inside .then() is not actually the response from this URL (Config+headerURL), but it is the response that was obtained from a different URL previously called in first Controller using the same APIService service.

只是为了通知:从当前的URL的实际响应都在后期到货

Just to inform: the actual response from current URL do arrive at a later stage.

我知道异步召唤的,我认为它有东西在这种情况下做的,但我想。那么()处理它棱角分明。所以,我很困惑的是这里的问题。任何人都可以提供一些线索吗?

I am aware of asynchronous callings and I think it has something to do in this case, but I guess .then() handles it in Angular. So I am confused what is the issue here. Can anyone shed some light please?

推荐答案

由于服务是一个单身,你只有递延对象的单个实例。

Since the service is a singleton, you only have a single instance of the deferred object.

一旦解决,将不断得到解决,因此,下一次你打电话,的getData,它会立即返回。

Once resolved, it will keep being resolved, so the next time you call, getData, it will return immediately.

您可以移动

var deferred = $q.defer();

你都和的getData功能POSTDATA里面。

inside both your getData and postData function.

或者你可以只返回一个HTTP $创建的承诺。

Or you could just return the promise that $http creates.

这篇关于AngularJS:为什么。然后()不等待承诺的对象到达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆