I return a promise object from a function and act on it.
function getUserName(guid) { return $.getJSON(urlCurrent, { "method" : "get_user_info", "guid" : guid, "auth_token" : temporaryAuthToken }); } getUserName(guid).done(function(data) { if (data.status == 0) { alert(data.result[0].name); } });
And, if you want to do a state check in front, then that's good.
function getUserName(guid) { return $.getJSON(urlCurrent, { "method" : "get_user_info", "guid" : guid, "auth_token" : temporaryAuthToken }).then(function(data){ return $.Deferred(function(def){ if (data.status == 0) { return def.resolve(data); } return def.reject(data); }); }); } getUserName(guid).done(function(data) { alert(data.result[0].name); });
source share