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

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

问题描述

为了进行 API 调用,我创建了以下 Angular 服务.

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");
    });

由于我的服务返回一个 promise 对象,所以我不希望 .then() 中的代码在服务数据到达之前工作.但是,它在服务数据到达之前运行(不知道如何).最奇怪的是,我在 .then() 中得到的响应实际上并不是来自这个 URL (Config+headerURL) 的响应,而是从之前在第一个 Controller 中使用相同的 URL 调用的不同 URL 获得的响应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.

我知道异步调用,我认为在这种情况下它可以做一些事情,但我猜 .then() 在 Angular 中处理它.所以我很困惑这里的问题是什么.任何人都可以请说明一下吗?

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:为什么 .then() 不等待 promise 对象到达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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