Reputation: 1260
Basically, my application is a PhoneGap app with windows azure service, I have a problem with callback method with javascript.Please check the code below.
checkeventsRegistration = function (azureEid,regid,callback) {
alert(azureEid + " <> " + regid);
var client = new WindowsAzure.MobileServiceClient('https://mobbacktest.azure-mobile.net/', 'RvahPxHKoEsGiLdlCYZpHBllvSVQxl66');
reglog = client.getTable('registrationlog');
query = reglog.where({ eventid: azureEid, regid: regid });
query.read().done(function (log)
{
alert("Log:" + log.length);
});
callback(log.length);
};
and i will call this function as below.
checkeventsRegistration(eid, regid, savenum);
My issue is this callback method is firing before the query().read()
.
Upvotes: 0
Views: 164
Reputation: 441
query.read().done(function (log)
{
alert("Log:" + log.length);
callback(log.length);
});
as the read function is a sync, the execution will go to the next line directly without waiting for the call result. on the other hand, .done() accepts two call, the first one onSuccess, and the second is the onError. Hence, you should call your callback function in done(function(){callback();})
Incase it was not worked, send the error, may be you need to JSON.stringfy(log) before making your process.
Upvotes: 1
Reputation: 136124
The purpose of the callback
in your outer function is to perform some action when the asynchronous operation is done, therefore you should be calling it from inside the done
method.
query.read().done(function (log){
alert("Log:" + log.length);
callback(log.length);
});
Upvotes: 2