Alex,
You are checking the length of the array before the deferred has been returned. One way to resolve this is to make your function a deferred as well:
getUsersByRole: function(portal, param) {
var users = []
var def = new Deferred;
portal.queryUsers(param).then(lang.hitch(this, function(items){
if (items && items.results.length > 0){
array.forEach(items.results, lang.hitch(this, function(item){
var u = {};
u.username = item.username;
u.fullName = item.fullName;
users.push(u);
}));
def.resolve(users);
}
}));
return def;
},
and then simply call your deferred function like this:
portalHandler.getUsersByRole(this._portal, {q: 'role:' + 'org_user'}).then(function(result){
console.info(result.length);
});