Thursday, 12 September 2013

JavaScript synchronous function

JavaScript synchronous function

I've got the below bit of code:
ApiClient.prototype.UserLogin = function(data, callback){
var url = baseURL+'users/login';
var params = {
method: 'POST',
body: data
};
function cb(success, response, code){
if (success === 1){
response = JSON.parse(response);
// Save User ID & Username to App Properites to be used for Auth.
Ti.App.Properties.setString("user_id", response.message._id);
Ti.App.Properties.setString("username",
response.message.username);
Ti.App.Properties.setString("password", data.password);
callback(response.success, response, code);
} else {
callback(success, response, code);
}
}
this._request(url, params, cb);
};
I'm getting cases where the callback callback(response.success, response,
code) is fired and the properties above have not been set.
Is it possible to make make the callback only fire one the properties have
been set?
Update to include how the function is called:
api.UserLogin({
'password' : password,
'username' : username
}, function(success, res, code) {
if (success == 1) {
// Do stuff..

No comments:

Post a Comment