简体   繁体   中英

Validating JSON response before another function call

var jsonData = Model.CheckData(); 

// If i get the response for then, i must pass the response to the following functions which are below.

if (!jsonData) {
    Ti.API.warn("JsonData");
    SendRes(jsonData);
}

The problem i am facing is, the functions are getting executed before i get the response at the first place. It should happen sequentially.

Note : I cannot call the function SendRes on my Json OnLoad , as its in a different class all together.

I assume Model.CheckData() is an AJAX call. You can give it a callback once ajax is done. You will have to modify that call to accept a callback:

//pass a callback to CheckData
Model.CheckData(function(jsonData){
    //do something with jsonData
    if (!jsonData) {
        Ti.API.warn("JsonData");
        SendRes(jsonData);
    }
});

//while in the CheckData function
function CheckData(callback){
    ...ajax call...
    //execute this on receive where "response" is your JSON
    callback(response);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM