I posted most of the same code in another question, but I have a different question here - how should I understand Javascript Asynchronous code?
function getUserStatus() {
var status;
function querySuccess(tx, results) {
var len = results.rows.length;
var row = results.rows.item(0);
console.log(row['id']);
status = {
question: row['id']
};
}
function errorCB(err) {
console.log(err);
}
db.transaction(function(tx) {
tx.executeSql('SELECT id FROM calculator ORDER by id ASC LIMIT 1', [], querySuccess, errorCB);
});
querySuccess();
console.log(status);
return status;
}
I understand that my code is executing before the variable status is being defined - but how do I stop processing until status is defined? I do not want this procedure to be asynchronous - if nothing is delivered from the database, then I don't want the operation to continue.
I am under the impression that I need to call querySuccess(), but what arguments would I pass to it?
I'm relatively new to Javascript and I haven't encountered this concept before, and I'm somewhat confused by how it works and how I can get my code to execute how I want.
querySuccessyourself, it gets called automatically when the query finishes. Put theconsole.log()call insidequerySuccess().querySuccess()function. You can't returnstatusfrom the function.