简体   繁体   English

AngularJS解决$ http承诺太晚了

[英]AngularJS resolving $http promise too late

I've written an AngularJS factory, profileService , that handles setting/getting user profile info. 我已经编写了一个AngularJS工厂profileService ,用于处理设置/获取用户配置文件信息。 Its getProfile method calls my web API, is returned a JSON object with the user's data, and resolves the $http.get promise with that data. 它的getProfile方法调用了我的Web API,返回了一个包含用户数据的JSON对象,并使用该数据解析了$http.get承诺。 This particular promise is being used in a login method which is a member of an authentication factory. 在身份验证工厂成员的login方法中使用了此特定的Promise。 The block of code within login is in the midst of returning its own $http.post promise (it's requesting a token). login中的代码块处于返回其自己的$http.post承诺(正在请求令牌)的过程中。 The idea is to retrieve and locally cache the user's profile data upon login. 这个想法是在登录时检索并在本地缓存用户的个人资料数据。

The issue is that the $http.get promise is resolving too late. 问题是$http.get承诺解决得太迟了。 That is, in my console output, the call shows as resolving to nothing ( undefined ), immediately followed by getProfile 's inline output showing a successful profile object retrieval. 也就是说,在我的控制台输出中,调用显示为未解决( undefined ),紧随其后的是getProfile的内联输出,显示成功检索了配置文件对象。 Calling a variable that's stored the resolved promise also yields undefined . 调用存储了已解决的Promise的变量也会产生undefined

Based on my research, I'm leaning towards the issue being me not thinking about promises correctly. 根据我的研究,我倾向于这个问题,因为我没有正确考虑承诺。 Any help in understanding where I went wrong is greatly appreciated! 非常感谢您了解我在哪里出错了!

Code: profileService.getProfile(userName): 代码:profileService.getProfile(userName):

profileService.getProfile = function(userName) {
    return $http.get(ntSettings.apiServiceBaseUri + '/api/Profile/GetProfile?userName=' + userName)
        .then(function(response) {
            return response.data;
        });
};

Code: authService.login() calling getProfile 代码:authService.login()调用getProfile

var profileService = $injector.get('profileService');
var userProfile = {};

profileService.getProfile(loginData.userName)
    .then(function(response) {
        userProfile = response;
        console.debug("authSvc -> login -> getProfile: Got profile data:", response.userProfile);
    }, function(error) {
        console.error("authSvc -> login: Unable to fetch profile", error);
    });
console.debug("authSvc -> login: Displaying profile object", response.userProfile);

Console debug output showing issues 控制台调试输出显示问题

authSvc -> login: Showing profile object: undefined
authSvc -> login -> getProfile: Got profile data: Object {FirstName: "John", LastName: "Doe", Birthday: "2015-05-06T04:00:00", Gender: 0, Location: "New York, NY"}

You need to put the logic to handle the result inside the .then() block, the "displaying profile object" logging is outside of the http-request-response part of the application. 您需要将处理结果的逻辑放在.then()块中,“显示配置文件对象”日志记录不在应用程序的http-request-response部分之外。 You can think of it as a different execution thread that is indepently run and no guarantees of 'before' can be assumed. 您可以将其视为独立运行的不同执行线程,并且不能假设“ before”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM