ajax - Jquery invoke $.when after a function finished -


i have following code:

ispublisher.done(function(isactive) {     if (isactive) {         addroletouser(inlineuid, "publisher");     }     $.when(setcountryandlanguage(), getuserroles()).done(setinlinemanualtracking); });  //add role inline manual table in db. function addroletouser(userid, role) {    return $.ajax({         type: "post",         url: "../publisher/service.asmx/addroletouser",         data: json.stringify({ id: userid, role: role }),         contenttype: "application/json; charset=utf-8",         datatype: "json"     }); } 

i want $.when occur after addroletouser function finished. how can achieve that?

i want $.when occur after addroletouser function finished.

that's current code does. assuming addroletouser starts asynchronous process, , want wait until process completes, addroletouser need accept callback or return promise.

if assume returns promise, can have call $.when wait resolve (using dummy promise instead if don't call it):

ispublisher.done(function(isactive) {     var p;     if (isactive) {         p = addroletouser(inlineuid, "publisher");     } else {         p = $.deferred().resolve().promise();     }     p.then(function() {         $.when(setcountryandlanguage(), getuserroles()).done(setinlinemanualtracking);     }); }); 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -