javascript - Order function call -
i need make submitadapterauthentication()
function work first getuserroles()
function, current implementation of getuserroles()
function being executed first submitadapterauthentication()
. how can fix this?
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() { } }); } }); } }); } // function obtain roles // should performed after submitadapterauthentication function getuserroles(){ var arrayroles = []; var attributes = wl.client.getuserinfo(realm, "attributes"); if(attributes){ if(attributes.roles){ arrayroles.push(attributes.roles); } } }
when chaining promises, if return promise then() callback, resulting promise resolved value undefined
.
in order make sure callbacks executed in order specified, make sure each callback returning promise @ end. if want return value callback, wrap in $q.when()
. in case looks not using intermediary return values, can wrap arbitrary value in $q.when() make sure promise returned:
checkonline().then(function(onl) { userobj.isloginonline = onl; return $q.when(true); }).then(function() { submitadapterauthentication(user, pass); return $q.when(true); }).then(function() {getuserroles();});
based on latest edit, looks ch.submitadapterauthentication()
returning promise. if case, should return promise function: return ch.submitadapterauthentication(invocationdata, {...
, return promise in callback: then(function() {return submitadapterauthentication(user, pass);})
if ch.submitadapterauthentication()
not return $q promise, have wrap yourself:
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;
Comments
Post a Comment