简体   繁体   English

麻烦在AngularJS中使用promise

[英]Troubles using promise with AngularJS

I'm currently working on an application based on node.js , the server side seems to be running properly but I'm having hard times with the public side. 我目前正在开发基于node.js的应用程序,服务器端似乎运行正常,但与公共端的日子很艰难。

I'm trying to use a promise with AngularJS in a controller . 我试图使用与控制器 AngularJS承诺 Here's my code : 这是我的代码:

var userId;
var userEmail = "somethingICanRecognizeInCaseItDoesntChange";

function getUserInfos(){
    var deferred = $q.defer();
    if(SessionStorageService.get('token')){
        Auth.isAlive().success(function(user){
            $scope.user = user;
            console.log("user :", user);
            console.log("user email :", user.email);
            deferred.resolve(user.email);
        });
    }
    else {
        $scope.user = {};
        deferred.resolve([]);
    }
    return deferred.promise;
}

userEmail = getUserInfos();
console.log("result :", userEmail);

I thought that result in console would be the same as user email , but it's not. 我认为控制台中的result将与user email相同,但事实并非如此。 Here's the console log : 这是控制台日志:

result : Object { $$state: Object }
user : Object { firstName: "super", lastName: "admin", email: "a", isSuperAdmin: true, _ts: "2016-05-03T11:17:06.943Z" } website-ranking.js:41:21
user email : a

Here's a screenshot of what I get by clicking on Object in my browser console : 这是我在浏览器控制台中单击“ Object得到的屏幕截图: 安慰

The problem is, whatever I try, I never have a as result. 问题是,无论我尝试,我从来没有a作为结果。 I tried things like getUserInfos().$$state , getUserInfos().$$state.value , getUserInfos().$$state.Object , getUserInfos().Object , getUserInfos().Object.value and none of these worked. 我尝试了诸如getUserInfos().$$stategetUserInfos().$$state.valuegetUserInfos().$$state.ObjectgetUserInfos().ObjectgetUserInfos().Object.value ,但这些都getUserInfos().Object.value

What do I miss here ? 我在这里想念什么? Am I using promise in a wrong way ? 我是否以错误的方式使用了诺言? Am I forgetting something ? 我忘记了什么吗?

Please don't hesitate to tell me if this question can be improved, I'll gladly add code, or any informations you may need to help me solve my problem. 请不要犹豫告诉我是否可以改进此问题,我很乐意添加代码,或者您可能需要帮助我解决问题的任何信息。

Edit : the thing is, I already used most of the code elsewhere, it's only since I added promise that I'm a bit lost. 编辑:事实是,我已经在其他地方使用了大多数代码,只是因为我添加了保证,我有点迷路了。 Here for example, I use the Auth.isAlive() without any problem : 例如,在这里,我使用Auth.isAlive()没有任何问题:

function getUserInfos(cb){
            if(SessionStorageService.get('token')){
                Auth.isAlive().success(function(user){
                    $scope.user = user;
                    cb();
                });
            }
            else {
                $scope.user = {};
                cb();
            }
        }

The $scope.user is modified and usable further in the application with no issue. $ scope.user已修改,可以在应用程序中进一步使用,没有问题。

As the question has been tagged as possible duplicate, I'd like to explain a bite more : I know why I'm using a promise, and I think I understood the differents issues with async in javascript. 由于该问题被标记为可能重复,因此我想进一步解释一下:我知道我为什么要使用诺言,而且我认为我了解javascript中async的不同问题。 Here my problem is that my code doesn't work and I can't understand why, though I already successfully used promises in the past. 这是我的问题,尽管我过去已经成功使用了Promise,但我的代码却无法正常工作,我也不明白为什么。 I'm not asking how to get a response from async call, but a little help understand where I went wrong while writting code. 我不是在问如何从异步调用中获取响应,而是可以帮助我理解编写代码时哪里出错了。 If you still think that's a duplicate, let me know in comments and I'll deal with it, it's possible that I missed something in the link provided. 如果您仍然认为这是重复项,请在评论中让我知道,然后我将进行处理,可能是我错过了所提供链接中的某些内容。

Because your method getUserInfos() returns a promise, you're able to do this: 由于您的方法getUserInfos()返回了一个Promise,因此您可以执行以下操作:

var userId;
var userEmail = "somethingICanRecognizeInCaseItDoesntChange";

function getUserInfos(){
    var deferred = $q.defer();
    if(SessionStorageService.get('token')){
        Auth.isAlive().success(function(user){
            $scope.user = user;
            console.log("user :", user);
            console.log("user email :", user.email);
            deferred.resolve(user.email);
        });
    }
    else {
        $scope.user = {};
        deferred.resolve([]);
    }
    return deferred.promise;
}

getUserInfos().then(function(_email){
  userEmail = _email;
  console.log("result :", userEmail);
});

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

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