Javascript 订单函数调用

Javascript 订单函数调用,javascript,angularjs,Javascript,Angularjs,我需要使用submitAdapterAuthentication()函数来运行第一个getUserRoles()函数,但是在当前执行getUserRoles()函数时,首先执行的是submitAdapterAuthentication()。我怎样才能解决这个问题 checkOnline().then(function(onl) { userObj.isLoginOnline = onl; }).then(function() { submitAda

我需要使用
submitAdapterAuthentication()
函数来运行第一个
getUserRoles()
函数,但是在当前执行
getUserRoles()
函数时,首先执行的是
submitAdapterAuthentication()
。我怎样才能解决这个问题

    checkOnline().then(function(onl) {
        userObj.isLoginOnline = onl;
    }).then(function() {
        submitAdapterAuthentication(user, pass);
    }).then(function() {
        getUserRoles();
    });


function submitAdapterAuthentication(user, pass) {
    var invocationData = {
        parameters : [ user, pass ],
        adapter : "adapterAuth",
        procedure : "submitLogin"
    };

    ch.submitAdapterAuthentication(invocationData, {
        onFailure : function(error) {
            WL.Logger.log("ERROR ON FAIL: ", error);
        },
        onSuccess : function() {
            WL.Client.updateUserInfo({
                onSuccess : function() {
                    //return promise
                    WL.Client.updateUserInfo({
                        onSuccess : function() {
                        }
                    });
                }
            });
        }
    });
}

 // my function to obtain roles 
    // It should be performed after submitAdapterAuthentication
    function getUserRoles(){
        var arrayRoles = [];
        var attributes = WL.Client.getUserInfo(realm, "attributes");
        if(attributes){
            if(attributes.roles){
                arrayRoles.push(attributes.roles);
            }
        }
    }

链接承诺时,如果从then()回调返回除另一个承诺以外的任何内容,则生成的承诺将立即解析为值
undefined

为了确保回调按照指定的顺序执行,只需确保每个回调在最后都返回一个承诺。如果要从回调返回一些值,请将其包装在
$q.when()
中。在这种情况下,看起来您没有使用任何中间返回值,因此您可以将任意值包装在$q中。when()确保返回承诺:

checkOnline().then(function(onl) {
    userObj.isLoginOnline = onl;
    return $q.when(true);
}).then(function() {
    submitAdapterAuthentication(user, pass);
    return $q.when(true);
}).then(function() {getUserRoles();});
根据您最近的编辑,它看起来像是
ch.submitAdapterAuthentication()
可能会返回一个承诺。如果是这种情况,您应该从函数返回此承诺:
返回ch.submitAdapterAuthentication(调用数据,{…
然后在随后的回调中返回此承诺:
then(function(){return submitAdapterAuthentication(user,pass);})

如果
ch.submitAdapterAuthentication()
未返回$q承诺,您必须自己包装它:

var deferred = $q.defer();
ch.submitAdapterAuthentication(invocationData, {
    onFailure : function(error) {
        WL.Logger.log("ERROR ON FAIL: ", error);
        deferred.reject(error);
    },
    onSuccess : function() {
        WL.Client.updateUserInfo({
            onSuccess : function() {
                deferred.resolve();
            }
        });
    }
});
return deferred.promise;

我试过了,但是getUserRoles()中的“attributes”只返回{},就好像submitAdapterAuthentication没有发生一样。我的submitAdapterAuthentication是Pablo,我看到你接受了答案,这是否意味着你已经开始工作了?如果你仍然有问题,请告诉我,我将在今晚晚些时候进一步研究。